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

发布于 2024-09-09 04:31:41 字数 2063 浏览 1 评论 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);
        }
    }

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

GmailAccount acc = new GmailAccount("zippoxer", "******", "Moshe");
acc.SendMessage("[email protected]", "Hello Self!", "like in the title...", "C:\\822d14ah857.r");

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

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

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

I use this class to send mails trough a gmail account:

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);
        }
    }

Here is an example of how I use it:

GmailAccount acc = new GmailAccount("zippoxer", "******", "Moshe");
acc.SendMessage("[email protected]", "Hello Self!", "like in the title...", "C:\\822d14ah857.r");

The last parameter in the SendMessage method is the location of an attachment I want to add.

I tried sending a mail with an attachment of 400KB, worked great (even 900KB works). But then I tried uploading an attachment of 4MB, didn't work. Tried 22MB -> didn't work too.

There should be a limit of 25MB per message in Gmail. My message's subject and body are almost empty so don't consider them as part of the message's size. Why do I have that low limit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-柠檬树下少年和吉他 2024-09-16 04:31:41

根据这篇文章,这是 .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-系统网络邮件-mailmessage

According to this post, it is a bug in .Net 4.0. The limit specified in the post is 3,050,417 bytes. You can try the work-around code included in the post. Hope this helps.

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

一袭白衣梦中忆 2024-09-16 04:31:41

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

It's still possible to send. Just change the attachment encoding to something other than Base64. I tried testing this and found that there is a IndexOutOfBoundsException in the Base64 encoding code. I was able to successfully send an 11MB file to myself using TransferEncoding.SevenBit.

悲念泪 2024-09-16 04:31:41

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

Check and see if the SmtpClient object is going out of scope or otherwise being disposed before the send is complete and has sent the QUIT to the server.

菊凝晚露 2024-09-16 04:31:41

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

Okay, this is a bug in .net 4.
Microsoft says it will be fixed in the next service pack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文