System.Web.Mail FAQ

Complete FAQ Listing


2.3 How do I send an email with attachments? Printer Friendly   Email This FAQ   Discuss

To send an email with attachments, the ASP.NET process (or the ASP.NET impersonated account) will need permission to read the file, and attach it to the MailMessage class.

Note: Attachments can only be created from files on the file system. System.Web.Mail does not support creating attachments directly from strings, byte arrays, streams, or from uploaded files. To directly create attachments from these types, use aspNetEmail, otherwise, the attachment contents must first be saved to the file system.

The following example demonstrates adding the file "test.txt" as an attachment to a MailMessage.
 
[ C# ]
MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); //create the attachment
mail.Attachments.Add( attachment );	//add the attachment
SmtpMail.SmtpServer = "localhost";  //your real server goes here
SmtpMail.Send( mail );

[ VB.NET ]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
Dim attachment As New MailAttachment(Server.MapPath("test.txt")) 'create the attachment
mail.Attachments.Add(attachment) 'add the attachment
SmtpMail.SmtpServer = "localhost" 'your real server goes here
SmtpMail.Send(mail)

 

Discuss This FAQ Item
Got a question? Another Suggestion? Want to post your solution? Discuss it below.
New Post
  • Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
      • RE RE: Multiple Attachments    [ Reply ]
        • RE: RE RE: Multiple Attachments    [ Reply ]
          • RE RE: RE RE: Multiple Attachments    [ Reply ]
            • RE: RE RE: RE RE: Multiple Attachments    [ Reply ]
              • RE RE: RE RE: RE RE: Multiple Attachments    [ Reply ]
          • RE RE: RE RE: Multiple Attachments    [ Reply ]
          • RE RE: RE RE: Multiple Attachments    [ Reply ]
        • RE: RE RE: Multiple Attachments    [ Reply ]
      • RE RE: Multiple Attachments    [ Reply ]
        • RE: RE RE: Multiple Attachments    [ Reply ]
        • RE: RE RE: Multiple Attachments    [ Reply ]
      • RE RE: Multiple Attachments    [ Reply ]
      • RE RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
      • RE RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
    • RE: Multiple Attachments    [ Reply ]
  • Mailing Memory    [ Reply ]
    • RE: Mailing Memory    [ Reply ]
  • Invalid Email Attachment    [ Reply ]
    • RE: Invalid Email Attachment    [ Reply ]
      • RE RE: Invalid Email Attachment    [ Reply ]
        • RE: RE RE: Invalid Email Attachment    [ Reply ]
          • RE RE: RE RE: Invalid Email Attachment    [ Reply ]
          • RE RE: RE RE: Invalid Email Attachment    [ Reply ]
    • RE: Invalid Email Attachment    [ Reply ]
      • RE RE: Invalid Email Attachment    [ Reply ]
        • RE: RE RE: Invalid Email Attachment    [ Reply ]
      • RE RE: Invalid Email Attachment    [ Reply ]
    • RE: Invalid Email Attachment    [ Reply ]
  • Attachment Size Limit?    [ Reply ]
    • RE: Attachment Size Limit?    [ Reply ]
      • RE RE: Attachment Size Limit?    [ Reply ]
        • RE: RE RE: Attachment Size Limit?    [ Reply ]
          • RE RE: RE RE: Attachment Size Limit?    [ Reply ]
            • RE: RE RE: RE RE: Attachment Size Limit?    [ Reply ]
            • RE: RE RE: RE RE: Attachment Size Limit?    [ Reply ]
              • RE RE: RE RE: RE RE: Attachment Size Limit?    [ Reply ]
    • RE: Attachment Size Limit?    [ Reply ]
      • RE RE: Attachment Size Limit?    [ Reply ]
    • RE: Attachment Size Limit?    [ Reply ]
      • RE RE: Attachment Size Limit?    [ Reply ]
  • Mail attachments permission error    [ Reply ]
  • Invalid Mail Attachment    [ Reply ]
    • RE: Invalid Mail Attachment    [ Reply ]
  • attachements types    [ Reply ]
    • RE: attachements types    [ Reply ]
  • exception leaving attachment locked    [ Reply ]
  • Cannot attach a file that I can copy    [ Reply ]
  • The attachment is failing    [ Reply ]
  • Want to attach file from network path    [ Reply ]
  • Invalid mail attachment (images only)    [ Reply ]
    • RE: Invalid mail attachment (images only)    [ Reply ]
  • How to Restrict Users    [ Reply ]
  • Authentication    [ Reply ]
  • File with Attachment sits in Queue    [ Reply ]
    • RE: File with Attachment sits in Queue    [ Reply ]
  • Email Attachments    [ Reply ]
  • Server.mappath error    [ Reply ]
    • RE: Server.mappath error    [ Reply ]
  • how to upload the attachment to email    [ Reply ]
  • abut Multi-Threading in ASP NET using C#    [ Reply ]
  • Change the name of an attachment.    [ Reply ]
    • RE: Change the name of an attachment.    [ Reply ]
  • sending image as stream inside email-body    [ Reply ]
  • create temp file in client side    [ Reply ]
  • Send url in mail message.    [ Reply ]
    • RE: Send url in mail message.    [ Reply ]
  • How to give a different name to attachment    [ Reply ]
  • How to give a different name to attachment    [ Reply ]
    • RE: How to give a different name to attachment    [ Reply ]
  • Problem: HTML email body and encoded attachment    [ Reply ]
  • optional attachment    [ Reply ]
    • RE: optional attachment    [ Reply ]
  • Awesome    [ Reply ]
  • Attachmen can't open after receiving email    [ Reply ]
  • question    [ Reply ]
  • Attaching file on client computer    [ Reply ]
  • Email with attachment published on IIS    [ Reply ]
    • RE: Email with attachment published on IIS    [ Reply ]
  • mail attachment    [ Reply ]
  • Attachments    [ Reply ]
  • PDF format    [ Reply ]
    • RE: PDF format    [ Reply ]
    • RE: PDF format    [ Reply ]
    • RE: PDF format    [ Reply ]
  • Sending emails    [ Reply ]
    • RE: Sending emails    [ Reply ]
  • Different attachments to diff ids    [ Reply ]
  • Regarding Smpt Time Out error while attaching files.    [ Reply ]
  • How to send mht attachment as email without embedded?    [ Reply ]
    • RE: How to send mht attachment as email without embedded?    [ Reply ]
  • Attach pdf file embedded    [ Reply ]
  • need to send 1 lacs of email with attachment    [ Reply ]
  • Thanks    [ Reply ]
  • How do I send an emai;?    [ Reply ]
    • RE: How do I send an emai;?    [ Reply ]
  • Thanks    [ Reply ]