6

我使用此类通过 gmail 帐户发送邮件:

public class GmailAccount
    {
        public string Username;
        public string Password;
        public string DisplayName;

        public string Address
        {
            get
            {
                return Username + "@gmail.com";
            }
        }

        private SmtpClient client;

        public GmailAccount(string username, string password, string displayName = null)
        {
            Username = username;
            Password = password;
            DisplayName = displayName;

            client = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(Address, password)
            };
        }

        public void SendMessage(string targetAddress, string subject, string body, params string[] files)
        {
            MailMessage message = new MailMessage(new MailAddress(Address, DisplayName), new MailAddress(targetAddress))
            {
                Subject = subject,
                Body = body
            };

            foreach (string file in files)
            {
                Attachment attachment = new Attachment(file);
                message.Attachments.Add(attachment);
            }

            client.Send(message);
        }
    }

这是我如何使用它的示例:

GmailAccount acc = new GmailAccount("zippoxer", "******", "Moshe");
acc.SendMessage("zippoxer@gmail.com", "Hello Self!", "like in the title...", "C:\\822d14ah857.r");

SendMessage方法中的最后一个参数是我要添加的附件的位置。

我尝试发送带有 400KB 附件的邮件,效果很好(甚至 900KB 也可以)。但是后来我尝试上传一个4MB的附件,没有用。尝试了 22MB -> 也没有用。

Gmail 中每封邮件的大小应限制为 25MB。我的邮件主题和正文几乎是空的,所以不要将它们视为邮件大小的一部分。为什么我有这么低的限制?

4

4 回答 4

5

根据这篇文章,这是.Net 4.0中的一个错误。帖子中指定的限制是 3,050,417 字节。您可以尝试帖子中包含的解决方法代码。希望这可以帮助。

http://connect.microsoft.com/VisualStudio/feedback/details/544562/cannot-send-e-mails-with-large-attachments-system-net-mail-smtpclient-system-net-mail-mailmessage

于 2010-07-15T00:56:09.183 回答
1

还是可以寄的。只需将附件编码更改为 Base64 以外的其他编码。我尝试对此进行测试,发现IndexOutOfBoundsExceptionBase64 编码代码中有一个。我能够使用TransferEncoding.SevenBit.

于 2010-07-15T01:27:53.673 回答
0

在发送完成并将 QUIT 发送到服务器之前,检查并查看 SmtpClient 对象是否超出范围或以其他方式被处置。

于 2010-07-15T00:40:53.160 回答
0

好的,这是 .net 4 中的一个错误。微软表示它将在下一个服务包中修复。

于 2010-07-15T12:31:33.690 回答