开发者

Cannot send emails with large attachments in VS 2010

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 se开发者_运维问答nd 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?


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("me@mycompany.com");
        mail.To.Add("you@yourcompany.com");

        //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).


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("user1@contoso.com", "user2@contoso.com", "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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜