【问题标题】:System.Net.Mail - Trying to send a mail with attachment to gmail, works but for small attachments onlySystem.Net.Mail - 尝试向 gmail 发送带有附件的邮件,但仅适用于小附件
【发布时间】:2010-07-15 00:09:10
【问题描述】:

我使用这个类通过 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。我的邮件主题和正文几乎是空的,所以不要将它们视为邮件大小的一部分。为什么我有这么低的限制?

【问题讨论】:

  • 在普通的邮件客户端中能用吗?
  • 是的,忘记说了。我刚刚通过 www.gmail.com 上传了一个 22MB 的文件。
  • 是否有错误信息,或者没有附件发送的电子邮件?您是从服务器还是本地运行它?您可以尝试测试更多文件大小以找到更精确的限制吗? (2MB?4MB?二分查找技术可行)
  • 就像我现在告诉 DaveWilliamson:“SmtpClient.Send() 抛出异常:发送邮件失败。”我在本地运行它(在 vs 2010 中调试),我会尝试找到更精确的限制。编辑 >> 有效的尺寸:1.2MB、2.0MB,(尝试更多..)

标签: c# .net c#-4.0 gmail system.net.mail


【解决方案1】:

根据这篇文章,这是 .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

【讨论】:

【解决方案2】:

仍然可以发送。只需将附件编码更改为 Base64 以外的其他编码。我尝试对此进行测试,发现 Base64 编码代码中有一个IndexOutOfBoundsException。我能够使用TransferEncoding.SevenBit 成功向自己发送一个 11MB 的文件。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 我不这么认为,因为当我尝试上传大文件时,它会立即告诉我发送失败。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2017-06-22
      • 2012-05-07
      • 2015-10-25
      • 2016-05-29
      • 2018-11-11
      • 2016-09-22
      相关资源
      最近更新 更多