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

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

 2012-11-15 16:04:30 来源:WEB开发网   
核心提示: public static void SendMail()//普通的邮件发送{MailAddress from = new MailAddress("FromEmail@website.com");MailAddress to = new MailAddress("ToEmail@web

 public static void SendMail()//普通的邮件发送
{
MailAddress from = new MailAddress("FromEmail@website.com");
MailAddress to = new MailAddress("ToEmail@website.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "邮件标题";
message.Body = "邮件内容";
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格式的Email发送
public static void SendMail()//带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>";
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
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的邮件发送
{
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>";
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(htmlBodyContent, null, "text/html");
message.AlternateViews.Add(htmlBody);

Attachment attachment = new Attachment("C:\\附件.rar");

1 2 3  下一页

Tags:ASP NET 使用

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