///建立 。多个邮件地址又“,”,“;”隔开
protected List<mailData> BuildMails()
{
List<mailData> mails = new List<mailData>(); //mailData是一个email的实体类。
CEmail email = new CEmail();
string[] temps = this.TBtoemail.Text.Split(',', ';');
string configStr = ConfigurationManager.AppSettings["recommendSubject"].ToString(); //邮件主题是配置文件配置的。
for (int i = 0; i < temps.Length; i++)
{
//实例化具体mail实例
if (!string.IsNullOrEmpty(temps[i].Trim()))
{
mailData mail = new mailData();
//如果地址中有字符
mail.Subject = string.Format(configStr, this.TBfriendname.Text, this.TBfromEmail.Text);
mail.Encode = string.Empty;
mail.SendTime = System.DateTime.Now;
mail.UserName = this.TBfriendname.Text;
mail.ApplicationName = "0";//0为好友推荐
mail.SendTime = DateTime.Now;
mail.Content = this.content.Value;
mail.FromMail = this.TBfromEmail.Text;
mail.IPOuter = Context.Request.UserHostAddress;
mail.ToEmail = temps[i];
if (CEmail.validatEmail(temps[i].Trim()))
{
mail.Available = true;
}
else
{
mail.Available = false;
}
mails.Add(mail);
}
}
return mails;
}2:具体的发送代码//多封邮件。参数是list类型。
public void RecommendSend(List<mailData> mails)
{
foreach (mailData mail in mails)
{
if (mail.Available)
RecommendSend(mail);
}
}
//邮件发送方法
public void RecommendSend(mailData mail)
{
try
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(
EmailFrom, mail.ToEmail, mail.Subject, mail.Content);
message.BodyEncoding = System.Text.Encoding.UTF8;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(EmailHostName); //mail.itingwang.com邮件服务器
client.Credentials = new System.Net.NetworkCredential(EmailUserName, EmailPassword);//"servermgr@itingwang.com", ".itisservermgr"
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//邮件通过网络发送到smtp服务器
client.Send(message);
mail.sendstatus = 1;
}
catch (Exception ex)
{
mail.sendstatus = 0; //sendstatus为0,sendstatus = -1; 邮件发送失败
mail.ErrorMsg = ex.Message;
ApplicationLog.WriteError(ex.Message);
}
saveMail(mail);
}