System.Net.Mail - Trying to send a mail with attachment to gmail, works but for small attachments only
Asked Answered
M

4

6

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?

Mckoy answered 15/7, 2010 at 0:9 Comment(4)
Does it work in a normal mail client?Thursby
Yeah, forgot to say that. I just uploaded a 22MB file trough www.gmail.com.Mckoy
Is there an error message, or does the e-mail get sent without the attachment? Are you running this from a server or locally? Can you try testing more file sizes to find a more precise limit? (2MB? 4MB? binary search technique may work)Dyann
Like I told DaveWilliamson right now: "SmtpClient.Send() throws an exception: Failure sending mail." I'm running it localy (debugging in vs 2010), and I will try find a more precise limit. Edit >> sizes that are working: 1.2MB, 2.0MB, (trying more..)Mckoy
D
5

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

Dyann answered 15/7, 2010 at 0:56 Comment(1)
Until the next service pack turns up..... A public patch is now available for this issue. You can find it here: connect.microsoft.com/VisualStudio/Downloads/…Lanza
E
1

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.

Emblazonry answered 15/7, 2010 at 1:27 Comment(0)
P
0

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.

Plication answered 15/7, 2010 at 0:40 Comment(1)
I don't think so because when I try uploading a large file it tells me immediately that the sending failed.Mckoy
M
0

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

Mckoy answered 15/7, 2010 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.