Sending Inline Attachments Thru E-Mail

The following is the code snippet to send inline attachment thru System.Net.Mail:

Include the namespace System.Net.Mail


1. Create a new MailMessage object:

MailMessage msg = new MailMessage(fromAddress, toAddress);

2. Create the LinkedResource:

LinkedResource logo = LinkedResource("c:\\test.jpeg");

3. Create the ContentId for the LinkedResource:

logo.Contentid = "CompanyLogo";

4. Create the AlternateView object and assign the required view:

AlternateView htmlView = AlternateView.CreateAlternateViewFromString("The embeded image is: ", null, "text/html");

5. Add the LinkedResource created in step2 to the AlternativeView created in step 4:

htmlView.LinkedResources.Add(logo);

6. Add the AlternativeView created in step 4 to the MailMessage object created in step 1:

msg.AlternativeViews.Add(htmlView);

7. Create SmtpClient object to send e-mail:

SmtpClient email = new SmtpClient();
email.UseDefaultCredentials = true;
email.Host = myHost; // Specify the host name
email.Send(msg);


Thanks,
Murali.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: