locked
How to send email with multiple attachments in c# RRS feed

  • Question

  • Hi

    How to send email with multiple attachments in c# .

    Thanks and Regards

    N.Ram

    Wednesday, February 27, 2013 8:48 AM

Answers

  • Hi chandran.nr,

    You can directly use MailMessage.Attachments Property to add multiple attachments when sending email, like:

    mail.Body = txtComments.Text;
    //Attach file
    mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments2.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments3.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments4.Text.ToString()));
    SmtpServer.Port = 587;
    ...

    The following code example demonstrates creating and sending an e-mail message with an attachment.

    public static void CreateMessageWithAttachment(string server) {

    // Specify the file to be attached and sent. // This example assumes that a file named Data.xls exists in the

    // current working directory. string file = "data.xls"; // Create a message and set up the recipients. MailMessage message = new MailMessage( "jane@contoso.com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet."); // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); // Add the file attachment to this e-mail message. message.Attachments.Add(data); //Send the message. SmtpClient client = new SmtpClient(server); // Add credentials if the SMTP server requires them. client.Credentials = CredentialCache.DefaultNetworkCredentials; try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString() ); } data.Dispose(); }

    Here are also some discussions about  send email with multiple attachments.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7ce33efc-521c-45fd-bae9-4bab86b764df

    http://www.codeproject.com/Articles/8528/ASP-NET-email-with-multiple-attachments

    http://forums.asp.net/t/1748724.aspx/1

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Thursday, February 28, 2013 4:12 AM
  • Here is code to create an email message with two attachments from file

    m_MailMessage = new MailMessage(); m_MailMessage.To.Add(to.Replace(';', ',')); // Support both ';' and ',' separators m_MailMessage.From = new MailAddress(from); m_MailMessage.Subject = subject; m_MailMessage.Body = body; m_MailMessage.IsBodyHtml = isBodyHtml; m_MailMessage.Priority = priority; attachment1 = pathToFirstAttachmentFile;

    attachment2 = pathToSecondAttachmentFile; // create and add first attachment to collection Attachment data = new Attachment(attachment1, MediaTypeNames.Application.Octet); m_MailMessage.Attachments.Add(data); // create and add second attachment to collection Attachment data2 = new Attachment(attachment2, MediaTypeNames.Application.Octet); m_MailMessage.Attachments.Add(data);

    I have given an example where attachments are files but the constructor is overloaded for other types of attachments as well

    • Proposed as answer by Sarathi R Wednesday, February 27, 2013 10:58 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Wednesday, February 27, 2013 9:54 AM
  • m_MailMessage.Attachments is not a single value (it would be called Attachement then). It is a collection. Basically a colelction is like an array that grows itself (has no fixed maximum size).

    So jsut add as many attachements using Attachments.Add() as you need. 1 or 100 makes no difference. The upper limit should be around 2 Billion Attachements (and most mail servers will jsut not take a mail that size anyway).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Thursday, February 28, 2013 6:30 AM

All replies

  • Use System.Net.Mail and the Attachments collection

    Attachment data = new Attachment(attachments, MediaTypeNames.Application.Octet);
    m_MailMessage.Attachments.Add(data);
    

    Wednesday, February 27, 2013 9:03 AM
  • Hi

    I am not able to understand .Please help me in detail.

    Thanks

    N.Ram

    Wednesday, February 27, 2013 9:21 AM
  • Here is code to create an email message with two attachments from file

    m_MailMessage = new MailMessage(); m_MailMessage.To.Add(to.Replace(';', ',')); // Support both ';' and ',' separators m_MailMessage.From = new MailAddress(from); m_MailMessage.Subject = subject; m_MailMessage.Body = body; m_MailMessage.IsBodyHtml = isBodyHtml; m_MailMessage.Priority = priority; attachment1 = pathToFirstAttachmentFile;

    attachment2 = pathToSecondAttachmentFile; // create and add first attachment to collection Attachment data = new Attachment(attachment1, MediaTypeNames.Application.Octet); m_MailMessage.Attachments.Add(data); // create and add second attachment to collection Attachment data2 = new Attachment(attachment2, MediaTypeNames.Application.Octet); m_MailMessage.Attachments.Add(data);

    I have given an example where attachments are files but the constructor is overloaded for other types of attachments as well

    • Proposed as answer by Sarathi R Wednesday, February 27, 2013 10:58 AM
    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Wednesday, February 27, 2013 9:54 AM
  • Hi chandran.nr,

    You can directly use MailMessage.Attachments Property to add multiple attachments when sending email, like:

    mail.Body = txtComments.Text;
    //Attach file
    mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments2.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments3.Text.ToString()));
    mail.Attachments.Add(new Attachment(txtAttachments4.Text.ToString()));
    SmtpServer.Port = 587;
    ...

    The following code example demonstrates creating and sending an e-mail message with an attachment.

    public static void CreateMessageWithAttachment(string server) {

    // Specify the file to be attached and sent. // This example assumes that a file named Data.xls exists in the

    // current working directory. string file = "data.xls"; // Create a message and set up the recipients. MailMessage message = new MailMessage( "jane@contoso.com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet."); // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); // Add the file attachment to this e-mail message. message.Attachments.Add(data); //Send the message. SmtpClient client = new SmtpClient(server); // Add credentials if the SMTP server requires them. client.Credentials = CredentialCache.DefaultNetworkCredentials; try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString() ); } data.Dispose(); }

    Here are also some discussions about  send email with multiple attachments.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7ce33efc-521c-45fd-bae9-4bab86b764df

    http://www.codeproject.com/Articles/8528/ASP-NET-email-with-multiple-attachments

    http://forums.asp.net/t/1748724.aspx/1

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Thursday, February 28, 2013 4:12 AM
  • m_MailMessage.Attachments is not a single value (it would be called Attachement then). It is a collection. Basically a colelction is like an array that grows itself (has no fixed maximum size).

    So jsut add as many attachements using Attachments.Add() as you need. 1 or 100 makes no difference. The upper limit should be around 2 Billion Attachements (and most mail servers will jsut not take a mail that size anyway).


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    • Marked as answer by Lisa Zhu Tuesday, March 5, 2013 10:57 AM
    Thursday, February 28, 2013 6:30 AM
  • Hai,

    I followed the same thing , but when i use it i'm getting error as "Expression doesn't produce a value".. any idea about it??

    Thanks

    Tuesday, June 26, 2018 6:01 AM