.NET写的Email可以群发邮件的实用函数

using System.Net.Mail;

protected void Page_Load(object sender, EventArgs e){
        
string strAllEmail = "sdf,sdf,sdf,";
        
string[] strEachEmail = strAllEmail.Split(',');
        
int i_max=strEachEmail.Length;
        
int i;
        
for (i = 0; i < i_max;i++ )
            SendEmail(strEachEmail[i].ToString());
}

    
private SendEmail(string astrEachEmail)  
    {
        
//发送
        string strSendEmail = "發送者郵件地址";
        
string strUserName = "申請人用戶名";
        
string strPassWord = "申請人密碼";
        
string strRecieveEMail = astrEachEmail;//"接收者郵件地址"
        string strSubject = "主題";
        
string strBody = "內容";
        System.Net.Mail.MailMessage msg 
= new System.Net.Mail.MailMessage();
        msg.To.Add(strRecieveEMail);
        
////附件處理
        //if (txtAttachment.Text.Trim().Length > 0)
        
//{
        
//    string FileName = @txtAttachment.Text.Trim();
        
//    System.Net.Mail.Attachment pAttachment = new Attachment(FileName);
        
//    msg.Attachments.Add(pAttachment);
        
//}
        msg.From = new MailAddress(strSendEmail) ;
      
        msg.Subject 
= strSubject;//邮件标题           
        msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
        msg.Body = strBody;//邮件内容
        msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
        msg.IsBodyHtml = false;//是否是HTML邮件
        msg.Priority = MailPriority.High;//邮件优先级

        SmtpClient client 
= new SmtpClient();
        client.Host 
= txtSmtp.Text.Trim();//为邮件服务器设置
        System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(strUserName, strPassWord);
        client.Credentials 
= basicAuthenticationInfo;

        
object userState = msg;
        
try
        {
            client.Send(msg);
            
//client.SendAsync(msg, userState);//简单一点儿可以client.Send(msg);
            Response.Write("成功发送到" + strRecieveEMail + "邮箱");
        }
        
catch (System.Net.Mail.SmtpException ex)
        {
            Response.Write(
"发送邮件" + strRecieveEMail + "出错");
            
throw ex;
        }
    } 
posted @ 2008-01-09 10:29  Athrun  阅读(1224)  评论(0编辑  收藏  举报