using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net.Mail;
using
System.Web;
namespace
MailSend
{
public
class
SendEmail
{
/// <summary>
/// 群发邮件函数
/// </summary>
/// <param name="subject">发送的主题</param>
/// <param name="body">发送的内容</param>
/// <param name="email_list">收件人列表,通过|来分割开来,可以自己定义</param>
/// <param name="File_Path">发送附件的地址,获取了路径后上传</param>
public
static
void
Send(
string
subject,
string
body,
string
email_list,
string
File_Path)
{
string
MailUser =
"XXX@qq.com"
;
string
MailPwd =
"你的密码"
;
string
MailName =
"测试"
;
string
MailHost =
"smtp.exmail.qq.com"
;
MailAddress
from
=
new
MailAddress(MailUser, MailName);
MailMessage mail =
new
MailMessage();
mail.Subject = subject;
mail.From =
from
;
string
address =
""
;
string
[] email = email_list.Split(
'|'
);
foreach
(
string
name
in
email)
{
if
(name !=
string
.Empty)
{
address = name;
mail.To.Add(
new
MailAddress(address));
}
}
mail.Body = body;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml =
true
;
mail.Priority = MailPriority.Normal;
if
(File_Path !=
""
)
{
mail.Attachments.Add(
new
Attachment(File_Path));
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
}
SmtpClient client =
new
SmtpClient();
client.Host = MailHost;
client.Port = 25;
client.UseDefaultCredentials =
false
;
client.Credentials =
new
System.Net.NetworkCredential(MailUser, MailPwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
////如果发送失败,SMTP 服务器将发送 失败邮件告诉我
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mail);
}
}
}