首页 > 代码库 > 邮件批量发送
邮件批量发送
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public string mailContent ;
protected void Page_Load(object sender, EventArgs e)
{
}
public void Send()
{
MailMessage mail = new MailMessage();
try
{
//设置邮件的标题
mail.Subject = "悠哉WebService报告";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
//设置邮件的发件人
string SendEmail = ConfigurationManager.AppSettings["SendEmail"].ToString();
MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //邮件的发件人
mail.From = Addressfrom;
/* 这里这样写是因为可能发给多个联系人,每个地址用 , 号隔开
*/
List<string> mailAddress = new List<string>();
foreach (string address in mailAddress)
{
if (address != string.Empty)
{
mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
}
}
//设置邮件的内容
mail.Body = mailContent;
//设置邮件的格式
mail.BodyEncoding = System.Text.Encoding.UTF8;
//mail.IsBodyHtml = true;
//设置邮件的发送级别
mail.Priority = MailPriority.Normal;
//发送通知
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
SmtpClient client = new SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以了
client.Host = "smtp.gmail.com";
client.Port = 25;
client.Timeout = 9999;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
//邮箱登陆名和密码
string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//发送email
client.Send(mail);
}
catch (Exception)
{
//当邮件发送失败,发送异常时 使用备用方法调用备用邮箱发送
SendBackUp();
}
finally
{
mail.Dispose();
}
}
public void SendBackUp()
{
MailMessage mail = new MailMessage();
try
{
//设置邮件的标题
mail.Subject = "悠哉WebService报告";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
//设置邮件的发件人
string SendEmail = System.Configuration.ConfigurationManager.AppSettings["SendEmail"].ToString();
MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //邮件的发件人
mail.From = Addressfrom;
/* 这里这样写是因为可能发给多个联系人,每个地址用 , 号隔开
*/
List<string> mailAddress = new List<string>();
foreach (string address in mailAddress)
{
if (address != string.Empty)
{
mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
}
}
//设置邮件的内容
mail.Body = mailContent;
//设置邮件的格式
mail.BodyEncoding = System.Text.Encoding.UTF8;
//mail.IsBodyHtml = true;
//设置邮件的发送级别
mail.Priority = MailPriority.Normal;
//发送通知
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
SmtpClient client = new SmtpClient();
//设置用于 SMTP 事务的主机的名称,填IP地址也可以了
client.Host = "smtp.gmail.com";
client.Port = 25;
client.Timeout = 9999;
client.UseDefaultCredentials = true;
client.EnableSsl = true;
//邮箱登陆名和密码
string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//发送email
client.Send(mail);
}
catch (Exception)
{
//当邮件发送失败,发送异常时 使用备用方法调用备用邮箱发送
Send();
}
finally
{
mail.Dispose();
}
}
}