• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、一般的邮件发送

public static void SendMail()//普通的邮件发送
{
    MailAddress from = new MailAddress("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    MailMessage message = new MailMessage(from, to);
    message.Subject = "邮件标题";
    message.Body = "邮件内容";
    MailAddress copy = new MailAddress("mailto:[email protected]");
    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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    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:[email protected] ");
    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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    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");
    message.Attachments.Add(attachment);

    MailAddress copy = new MailAddress("mailto:[email protected]");
    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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");

    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:[email protected]");
    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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");
    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");

    MemoryStream imgStream = GetImage();
    imgStream.Position = 0;//非常重要,否则图片为空!在邮件里显示红叉
    LinkedResource lrImage = new LinkedResource(imgStream, 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:[email protected]");
    message.CC.Add(copy);
    SmtpClient client = new SmtpClient("SMTP Server");

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

public static MemoryStream GetImage()
{
    //Code here... ...
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.netcore错误页面处理【转载】发布时间:2022-07-10
下一篇:
在ASP.NET的datagrid中使用radiobutton的历程发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap