如何在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 " );
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 <img src='cid:logo'> will map to a LinkedResource with a ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlView1 + " <a href = "" ><img src="cid:logo" /></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);
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 <img src='cid:logo'> will map to a LinkedResource with a ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlView1 + " <a href = "" ><img src="cid:logo" /></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);