How to send embedded image emails in ASP. NET

Source: Internet
Author: User
Tags mailmessage smtpclient

How to send embedded image emails in ASP. NET

using System.Net.Mail;using System.Net.Mime;                MailMessage mail = new MailMessage();        //set the addresses        mail.From = new MailAddress("xx@xxx.com");        mail.To.Add("xx@xxx.com");        //set the content        mail.Subject = "Test";//first we create the Plain Text part        AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my text , viewable by those clients that don't support html", null, "text/plain");        string htmlView1 = "This is my text , viewable by those clients that don't support html";        //then we create the Html part        //to embed images, we need to use the prefix 'cid' in the img src value        //the cid value will map to the Content-Id of a Linked resource.        //thus  will map to a LinkedResource with a ContentId of 'companylogo'        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlView1 + "<a href = "" ></a>", null, System.Net.Mime.MediaTypeNames.Text.Html);        //create the LinkedResource (embedded image)        string path = Server.MapPath(@"Images/logo.jpg");      //  LinkedResource logo = new LinkedResource(path);        LinkedResource logo = new LinkedResource(path, "image/jpeg");        logo.ContentId = "logo";        //add the LinkedResource to the appropriate view        htmlView.LinkedResources.Add(logo);        //add the views        mail.AlternateViews.Add(plainView);        mail.AlternateViews.Add(htmlView);        mail.IsBodyHtml = true;        //send the message        SmtpClient smtp = new SmtpClient();//"127.0.0.1"); //specify the mail server address        smtp.Send(mail);

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.