VS发送邮件基础方法(可群发带附件)
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="ReceiveAddressList">收件人列表</param>
/// <param name="Subject">主题</param>
/// <param name="Content">内容</param>
/// <param name="AttachFile">附件<附件名称,附件服务器物理路径></param>
private void SendEmail(List<string> ReceiveAddressList, string Subject, string Content, Dictionary<string, string> AttachFile)
{
//smtp客户端
SmtpClient smtp = new SmtpClient("服务器地址");
//发件人邮箱身份验证凭证。 参数分别为 发件邮箱登录名和密码
smtp.Credentials = new NetworkCredential("登录名", "密码");
//创建邮件
MailMessage mail = new MailMessage();
//主题编码
mail.SubjectEncoding = Encoding.GetEncoding("GB2312");
//正文编码
mail.BodyEncoding = Encoding.GetEncoding("GB2312");
//邮件优先级
mail.Priority = MailPriority.Normal;
//以HTML格式发送邮件,为false则发送纯文本邮箱
mail.IsBodyHtml = false;
//发件人邮箱
mail.From =new MailAddress("发件人邮箱");
//添加收件人,如果有多个,可以多次添加
if (ReceiveAddressList.Count == 0)
{
return;
}
else
{
for (int i = 0; i < ReceiveAddressList.Count; i++)
{
mail.To.Add(ReceiveAddressList[i].ToString());
}
//邮件主题和内容
mail.Subject = Subject;
mail.Body = Content;
//定义附件,参数为附件文件名,包含路径,推荐使用绝对路径
foreach (string skey in AttachFile.Keys)
{
System.Net.Mail.Attachment objFile = new System.Net.Mail.Attachment(AttachFile[skey].ToString());
objFile.Name = skey;
mail.Attachments.Add(objFile);
}
try
{
//发送邮件
smtp.Send(mail);
}
catch (Exception)
{
throw;
}
finally
{
smtp.Dispose();
}
}
}
————————————————
版权声明:本文为CSDN博主「不懂love」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq719365064/article/details/52925294