System.Web.Mail 邮件正文显示图片

String FromAddress = "[email protected]";
//(1)创建MailMessage实例
MailMessage mm = new MailMessage(FromAddress, emailadd);
mm.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
mm.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
//(2)属性MailMessage赋值
mm.Subject = "title";

string strFilePath = @"E:\001.gif";

System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(strFilePath);//添加附件
attachment1.Name = System.IO.Path.GetFileName(strFilePath);
attachment1.NameEncoding = System.Text.Encoding.GetEncoding("gb2312");
attachment1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
attachment1.ContentDisposition.Inline = true;
attachment1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
string cid = attachment1.ContentId;//关键性的地方,这里得到一个id数值
mm.Attachments.Add(attachment1);

mm.Body = “Test <img src=cid:”+cid+" />"; //这里就是插入图片的方法
mm.IsBodyHtml = true;
//(3)创建SmtpClient对象
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.xxx.com";//邮件服务器
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("username", "pwd");//用户名和密码
try
{
smtp.Send(mm);//发送
return true;
}
catch(SmtpException se)
{
return false;
}

你可能感兴趣的:(System)