Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 631
Zippo Avatar asked Jul 15 '10 00:07

Zippo


People also ask

How do I change the attachment settings in Gmail?

Click on G-Suite. Scroll down to find Gmail and click on it. Scroll down to Advanced settings and click on it. Scroll down to Attachment compliance and hover on it and to see configure option and click on it.

Why are emails with attachments not sending?

The most common reason that an attachment won't send is that it is too big. These limits are set by whoever you use for email, whether it's an email account through your ISP or through an online provider like Yahoo or GMail. You should check with your email service provider to see what the limits are for attachments.

Why is my Gmail not sending emails with attachments?

Disable the Web Browser Proxy. If you've set up a web browser proxy, this might be causing the issue with Gmail can't send emails with attachments. So you should try disabling the proxy server.

What is the maximum attachment size for Gmail?

You can send up to 25 MB in attachments. If you have more than one attachment, they can't add up to more than 25 MB. If your file is greater than 25 MB, Gmail automatically adds a Google Drive link in the email instead of including it as an attachment.


2 Answers

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

like image 97
James Lawruk Avatar answered Oct 17 '22 01:10

James Lawruk


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.

like image 28
Jeff Mercado Avatar answered Oct 16 '22 23:10

Jeff Mercado