无法在 VS 2010 中发送带有大附件的电子邮件

发布于 2024-09-24 00:51:53 字数 684 浏览 6 评论 0 原文

我已经浏览过这个链接。 (http://connect.microsoft.com/VisualStudio/feedback/details/544562/cannot-send-e-mails-with-large-attachments-system-net-mail-smtpclient -system-net-mail-mailmessage)

在 .NET Framework 4.0 中,无法发送附件大于 4 MB 的电子邮件。如果将目标平台从 .NET Framework 4.0 设置为 .NET Framework 3.5,则相同的代码适用于小文件和大文件。所以这不可能是我们的邮件配置的问题!如果我附加例如 10 个 2 MB 的文件,我不会收到任何错误!我通过谷歌搜索但没有得到它。

解决方案未按预期正常工作。使用此解决方法一段时间后,我发现某些文件已损坏。所以这不是这个错误的解决方案。

我们已经应用了 Microsoft 补丁,但问题仍然存在?
有人可以告诉我如何解决这个问题吗?

I’ve gone through this link. (http://connect.microsoft.com/VisualStudio/feedback/details/544562/cannot-send-e-mails-with-large-attachments-system-net-mail-smtpclient-system-net-mail-mailmessage)

It is not possible to send an e-mail with an attachment larger than 4 MB in .NET Framework 4.0. The same code works for small and large files if you set the target platform from .NET Framework 4.0 to .NET Framework 3.5. So this cannot be a problem with our mail-configuration! I get no error if I attach e.g. 10 files of 2 MB! I searched through Google but I didn’t get it.

Workaround solution is not working fine as expected. After using this workaround for a while, I found that some files are corrupted. So this is not a solution for this bug.

We’ve applied that Microsoft patch and we’re still seeing the issue?
Can someone tell me how to fix this?

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

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

发布评论

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

评论(2

难以启齿的温柔 2024-10-01 00:51:54

这可能是迄今为止客户针对 .NET 4.0 Framework 中的 System.Net.Mail 类报告的第一个错误,或者至少是我处理的第一个错误。这是非常简单的重现,我不需要做太多事情就可以在本地重现该问题。

 static void Main(string[] args)

    {

        SmtpClient client = new SmtpClient("contoso_smtp_server");
        client.Credentials = new System.Net.NetworkCredential("User1", "Password", "contoso");


        MailMessage msg = new MailMessage("[email protected]", "[email protected]", "Large Attachment Mail", "Large Attachment - Test Body");

        Attachment attachment = new Attachment(@"d:\3mb.dat");
        msg.Attachments.Add(attachment);

        client.Send(msg);


    }

这是您可以编写的使用 SNM 发送电子邮件的最简单的代码,但问题是它失败并显示“发送电子邮件时出错”消息。因此,我环顾四周,发现问题与 SNM 没有直接关系,而是与它的底层类相关,特别是 Base64Encoding 类,该类用作发送时对电子邮件附件进行编码的默认方法。

这节省了我更多的故障排除时间,并且我将附件的编码方式从 Base64 更改为 7Bit,效果非常好。

因此,您需要做的就是将以下任意行添加到您的代码中以使其正常工作。

这两个代码部分中的任何“一个”都可以工作

attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

attachment.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;

此解决方案位于 此帖子

This is probably the first bug reported by customer so far for System.Net.Mail Class in .NET 4.0 Framework, or at least the first one I worked on. This was pretty straight forward repro and I did not had to do much to reproduce the issue locally.

 static void Main(string[] args)

    {

        SmtpClient client = new SmtpClient("contoso_smtp_server");
        client.Credentials = new System.Net.NetworkCredential("User1", "Password", "contoso");


        MailMessage msg = new MailMessage("[email protected]", "[email protected]", "Large Attachment Mail", "Large Attachment - Test Body");

        Attachment attachment = new Attachment(@"d:\3mb.dat");
        msg.Attachments.Add(attachment);

        client.Send(msg);


    }

That was the simplest code you could possibly write to send out email using SNM but the problem is it Fail with an “Error in sending email” message. So I looked around what was happening and found that the problem was not directly related to SNM but its underlying classes and specifically the Base64Encoding class which was used as default method of encoding emails attachments while sending.

That saved me more troubleshooting and I changed the way the attachments were being encoded from Base64 to 7Bit and it worked like charm.

So all you need to do is add any of the following line to your code to make it work.

Any "one" of those two code section will work

attachment.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;

attachment.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;

This solution was found in this post

怪我太投入 2024-10-01 00:51:53

使用 SMTP 拾取目录的可能解决方法

我不知道该错误是否存在于通过 SMTP 发送邮件的代码中或存在于带有大附件的序列化 MailMessage 中。如果它在发送中并且序列化正常,您可以尝试通过使用分拣目录发送来克服它。

类似这样:

        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is the body content of the email.";

        //if we are using the IIS SMTP Service, we can write the message
        //directly to the PickupDirectory, and bypass the Network layer
        SmtpClient smtp = new SmtpClient();
        smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
        smtp.Send(mail);

您需要在代码运行的同一台计算机上运行 Microsoft SMTP 服务器(Microsoft IIS、Microsoft Exchange)。

替代解决方案:

使用没有附件大小限制的第三方 SMTP 组件可能是一种方法(我们的 Rebex Secure Mail .NET 组件 就是此类 SMTP 库的示例)。

Possible workaround using SMTP Pickup Directory

I don't know if the bug is in code which sends the message via SMTP or in serialization MailMessage with large attachments. If it's in the sending and serialization is ok you might try to overcome it by using sending via the Pickup Directory.

Something like this:

        //create the mail message
        MailMessage mail = new MailMessage();

        //set the addresses
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");

        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is the body content of the email.";

        //if we are using the IIS SMTP Service, we can write the message
        //directly to the PickupDirectory, and bypass the Network layer
        SmtpClient smtp = new SmtpClient();
        smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
        smtp.Send(mail);

You would need a running Microsoft SMTP server (Microsoft IIS, Microsoft Exchange) on the same machine as your code runs.

Alternative solution:

Using a third party SMTP component which does not have attachment size limitation might be a way to go (our Rebex Secure Mail .NET component is example of such SMTP library).

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