Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send mail with attachment

Tags:

c#

asp.net

Edit: I'm able to send mail without attachment

Getting this error while trying to send mail:

System.Net.Mail.SmtpException: The operation has timed out.

Following is my code:

public static void SendMailMessage(string to, string subject, string body, List<string> attachment)
{
    MailMessage mMailMessage = new MailMessage();
    // string body; --> Compile time error, body is already defined as an argument

    mMailMessage.From = new MailAddress("[email protected]");

    mMailMessage.To.Add(new MailAddress(to));

    mMailMessage.Subject = subject;

    mMailMessage.Body = body;

    foreach (string s in attachment)
    {
        var att = new Attachment(s);

        mMailMessage.Attachments.Add(att);

    }


    // Set the format of the mail message body as HTML
    mMailMessage.IsBodyHtml = true;
    // Set the priority of the mail message to normal
    mMailMessage.Priority = MailPriority.High;

    using (SmtpClient mSmtpClient = new SmtpClient())
    {
        mSmtpClient.Send(mMailMessage);
    }
}

Web Config

 <system.net>
<mailSettings>
  <smtp from="mailid">
    <network host="smtp.gmail.com" port="587" enableSsl="true" userName="username" password="pass" />
  </smtp>
</mailSettings>

Note : Attachments not exceeding its limit(below than 25 mb)

What can I do to solve this problem, or what am I missing?

like image 208
Siz S Avatar asked Mar 06 '13 14:03

Siz S


People also ask

Is it possible to send attachment file along with mail?

Next to To..., type the e-mail address you want to send to. Click on the icon of the paperclip to attach message or go to Insert->File This will bring up a window where you can browse your folders and select the file(s) you want to attach.


1 Answers

So basically we discovered during the chat that the problem occurs because the upload of the attachments takes to long.

One way to solve it is to increase the timeout value of the SmtpClient:

mSmtpClient.Timeout = int.MaxValue;

Note: Use int.MaxValue for testing but use a more realistic value for the deployed solution.

like image 139
TimothyP Avatar answered Oct 30 '22 06:10

TimothyP