C#批量发送抄送邮件、附件
**
以下代码发送效果在⽹易和腾讯邮箱显⽰不⼀样
C#批量发送、抄送、密送、回执、邮件、附件
**
代码⽅法
using System;
using System.Net.Mail;
using System.Net;
namespace Tool
{
public class Mail
{
#region 发送邮件
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="LoginAccount">邮箱登录账号</param>
/// <param name="pwd">邮箱密码 or 客户端授权码</param>
/// <param name="SendName">发件⼈名称收件⼈邮箱列表显⽰名称</param>
/// <param name="Subject">邮件主题</param>
/// <param name="Body">邮件内容已启⽤html</param>
/
// <param name="ReciveAddrList">收件⼈列表{{"姓名","邮箱地址"},{"姓名","邮箱地址"}}</param>
/// <param name="CCAddrList">抄送⼈列表{{"姓名","邮箱地址"},{"姓名","邮箱地址"}}</param>
/// <param name="BBAddrList">秘秘抄送⼈列表</param>
/// <param name="HeadersAddrList">回执⼈列表</param>
/// <param name="AttachFile">附件列表</param>
/// <param name="High">邮件级别</param>
/// <param name="EnableSsl">SSL加密默认开启</param>
/// <param name="SMTP">SMTP协议地址默认163邮箱可以⾃⾏⽹上查询填写</param>
/// <returns></returns>
public static bool SendEmail(string LoginAccount, string pwd, string SendName, string Subject, string Body, Dictionary<string, string> ReciveAddrList, Dictionary<string, string> CCAddrList, List<string> BccAddrList, List<string> HeadersAddrList, Dictionary<string, string> AttachFile, MailPri
ority High = MailP riority.Normal, bool EnableSsl = true, string SMTP = "smtp.163")
{
using (SmtpClient client = new SmtpClient(SMTP))社区党建工作总结
{
if ((ReciveAddrList == null) || (ReciveAddrList.Count == 0)) //未填写收件⼈地址
{
return false;
}
else
{
#region 邮件基本配置
户籍地址是填什么地址client.UseDefaultCredentials = true;
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//通过SMTP协议发送
client.Credentials = new System.Net.NetworkCredential(LoginAccount, pwd);//验证⾝份
client.EnableSsl = EnableSsl;//SLL加密
MailMessage Email = new MailMessage();//建⽴邮件
Email.From = new System.Net.Mail.MailAddress(LoginAccount, SendName);//发送邮件邮件和显⽰的名字
Email.Subject = Subject;//邮件主题
Email.Body = Body;//邮件正⽂
Email.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码
Email.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
Email.Priority = High;//邮件优先级
Email.IsBodyHtml = true;//是否启⽤html
#endregion
#endregion
#region 逐⼀添加收件⼈
foreach (KeyValuePair<string, string> kvp in ReciveAddrList)//逐⼀添加收件⼈
{
Email.To.Add(new MailAddress(kvp.Value, kvp.Key, Encoding.UTF8));
}
#endregion
#region 添加附件
if (AttachFile != null)//添加附件
{
foreach (string key in AttachFile.Keys)
{
Attachment file = new Attachment(AttachFile[key]);
file.Name = key;
Email.Attachments.Add(file);
}
}
#endregion
范雨林
#region 逐⼀添加抄送⼈
foreach (KeyValuePair<string, string> kvp in CCAddrList)//逐⼀添加抄送⼈
{
Email.CC.Add(new MailAddress(kvp.Value, kvp.Key, Encoding.UTF8));
}
#endregion张宇和十一郎
#region 逐⼀添加秘秘抄送⼈
foreach (string BccAddr in BccAddrList)//逐⼀添加秘秘抄送⼈
{
Email.Bcc.Add(BccAddr);
}
#endregion
#region 逐⼀添加回执⼈员
foreach (string Headers in HeadersAddrList)//逐⼀添加回执⼈员
{
Email.Headers.Add("Disposition-Notification-To", "<" + Headers + ">");
}
#endregion
try
{
client.Send(Email);//正式发邮件
client.Dispose();
Email.Dispose();
}
catch
{
return false;
}
}
}
return true;
刘涛主演电视剧}
#endregion
}
}
调⽤⽅法
Tool.Mail.SendEmail(
"发送邮箱账号@163",//发送账号
"密码",// 如果是⽹易这⾥写客户端授权码不是的话写登录账号密码
"标题",//别⼈收件列表上显⽰的名称
"邮件主题",
"内容测试-发邮件、抄送⼈、密送⼈、回执⼈、附件",//邮件内容
new Dictionary<string, string> { { "腾讯-邮箱", "123456789@qq" }, { "腾讯-邮箱", "123456789@qq" } }, // 收信⼈列表
new Dictionary<string, string> { { "收信⼈名称", "收信⼈邮箱地址" }, { "收信⼈名称", "收信⼈邮箱地址" } },//抄送⼈列表
new List<string> { "密送⼈邮箱地址" },//密送⼈列表刘奕君和宋佳怎么回事
new List<string> { "回执⼈邮箱地址", "回执⼈邮箱地址" },//回执⼈列表
new Dictionary<string, string> { { "考核系统", "C:\\201903141457100864.xlsx" }, { "绩效考核.xlsx", "C:\\201903131116394094.xlsx" } }, MailPriority.
new Dictionary<string, string> { { "考核系统", "C:\\201903141457100864.xlsx" }, { "绩效考核.xlsx", "C:\\201903131116394094.xlsx" } }, MailPriority. Normal);//发送附件