System.Net.Mail邮件发送抄送附件(多个)

/// <summary>
/// 邮件发送抄送附件
/// </summary>
/// <param name="mailTo">收件人(可多个)</param>
/// <param name="mailCC">抄送人(可多个)</param>
/// <param name="mailSubject">主题</param>
/// <param name="mailbody">正文</param>
/// <returns>bool</returns>
public static bool SendEmail( List<string> mailTo, List<string> mailCC, string mailSubject, string mailbody,List<string> Attachurl)
{

// 设置发送方的邮件信息
string smtpServer = "smtp.com"; //SMTP服务器
string mailFrom = "XXX"; //登陆用户名
string userPassword = "XXX";//登陆密码

// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码

MailMessage mailMessage = new MailMessage();
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailbody;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = System.Net.Mail.MailPriority.High;//优先级
mailMessage.From = new MailAddress(mailFrom);//发送人

for (int i = 0; i < mailTo.Count; i++)
{
mailMessage.To.Add(new MailAddress(mailTo[i]));//收件人
}
for (int j = 0; j < mailCC.Count; j++)
{
mailMessage.CC.Add(new MailAddress(mailCC[j]));//抄送人
}

for (int k = 0; k < Attachurl.Count; k++)
{
var Path="文件路径";
var attach = new Attachment(Path);
attach.Name = "文件的显示名";
mailMessage.Attachments.Add(attach);//附件

}

try
{
smtpClient.Send(mailMessage); // 发送邮件
return true;
}
catch (SmtpException)
{
return false;
}

}

posted on 2020-09-01 13:36  小小先生、  阅读(355)  评论(0编辑  收藏  举报

导航