WEB开发网
开发学院WEB开发ASP.NET ASP.NET中使用System.Net.Mail发邮件 阅读

ASP.NET中使用System.Net.Mail发邮件

 2012-11-15 16:04:30 来源:WEB开发网   
核心提示:message.Attachments.Add(attachment);MailAddress copy = new MailAddress("mailto:CopyToEmail@website.com");message.CC.Add(copy);SmtpClient client = new
message.Attachments.Add(attachment);

MailAddress copy = new MailAddress("mailto:CopyToEmail@website.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient("SMTP Server");

client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}

四、邮件为HTML,而且带图片
public static void SendMail()//带HTML,并且HTML中有图片的邮件发送
{
MailAddress from = new MailAddress("FromEmail@website.com");
MailAddress to = new MailAddress("ToEmail@website.com");

MailMessage message = new MailMessage(from, to);
message.Subject = "邮件标题";

string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>";
htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a><img src=\"cid:imageInEmail\"/>";
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");

LinkedResource lrImage = new LinkedResource("C:\\图片.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg);
lrImage.ContentId = "imageInEmail"; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ,如果设置不正确,请不会显示图片
htmlBody.LinkedResources.Add(lrImage);

message.AlternateViews.Add(htmlBody);

MailAddress copy = new MailAddress("mailto:CopyToEmail@website.com");
message.CC.Add(copy);
SmtpClient client = new SmtpClient("SMTP Server");

client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
}

五、图片为动态生成
public static void SendMail()//带HTML,并且HTML中有图片的邮件发送
{
MailAddress from = new MailAddress("FromEmail@website.com");
MailAddress to = new MailAddress("ToEmail@website.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "邮件标题";

string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainTextBody, null, "text/plain"));
string htmlBodyContent = "如果你的看到<b>这个</b>,说明你是在以 <span style=\"color:red\">HTML</span> 格式查看邮件<br><br>";
htmlBodyContent += "<a href=\"http://www.baidu.com//">百度一下</a><img src=\"cid:imageInEmail\"/>";
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");

Tags:ASP NET 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接