System.Net.Mail - 尝试发送带有附件的邮件到gmail,但仅适用于小附件

Mos*_*vah 6 .net c# gmail system.net.mail c#-4.0

我使用这个类通过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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

以下是我如何使用它的示例:

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

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

我尝试发送一封400KB附件的邮件,工作得很好(甚至900KB工作).但后来我尝试上传4MB的附件,没用.试了22MB - >也没用.

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

Jam*_*ruk 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