C#批量发送邮件程序中用到的两个函数


/// <summary>
/// 发送单封邮件
/// </summary>
/// <param name="MailToAddress"></param>
  public void SendOneEmail(string MailToAddress)
  { 
   string MailFromAddress=txtEmail.Text;   
   string MailTitle=txtEmailTitle.Text;
   string MailContent=txtEmailContent.Text;
   string MailSmtpServer=textBoxsmtp.Text; 
   int TempMailServer=MailFromAddress.IndexOf("@",0);
   string MailUserName=MailFromAddress.Substring(0,TempMailServer); 
   string MailPassword=txtPassword.Text;  
   //string MailDdomain=MailFromAddress.Substring((TempMailServer+1),(MailFromAddress.Length-TempMailServer-1));
   try
   {
    //实例化MailMessage对象
    System.Web.Mail.MailMessage mail=new System.Web.Mail.MailMessage(); 
    //定义邮件的发送地址
    mail.From=MailFromAddress;
    //定义邮件的接收地址
    //设置以分号分隔的收件人电子邮件地址列表
    mail.To=MailToAddress;
    //定义邮件的主题
    mail.Subject=MailTitle;
    //设置电子邮件正文的内容类型
    //在这里我们以HTML的格式发送
    mail.BodyFormat=System.Web.Mail.MailFormat.Html;
    //设置电子邮件的正文
    mail.Body=MailContent; 
    //SMTP服务器
    //许多SMTP服务器都需要身份验证
    System.Web.Mail.SmtpMail.SmtpServer=MailSmtpServer;
    //验证
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
    //登陆名
    mail.Fields.Add("<A href="http://schemas.microsoft.com/cdo/configuration/sendusername",MailUserName">http://schemas.microsoft.com/cdo/configuration/sendusername",MailUserName); 
    //登陆密码
    mail.Fields.Add("<A href="http://schemas.microsoft.com/cdo/configuration/sendpassword",MailPassword">http://schemas.microsoft.com/cdo/configuration/sendpassword",MailPassword); 
    //发送
    System.Web.Mail.SmtpMail.Send(mail);
    listBox2.Items.Add(MailToAddress);
    txtSCount.Text=listBox2.Items.Count.ToString();   
    string strEmailSuccessFileName=Application.StartupPath+"//EmailSuccess.txt";
    if (File.Exists(strEmailSuccessFileName))
    {
     StreamWriter strwriterobj=File.AppendText(strEmailSuccessFileName);     
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.Close();    
    }
    else
    {
     StreamWriter strwriterobj=File.CreateText(strEmailSuccessFileName);  
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.Close();
    } 
   }
   catch(Exception ee)
   {
    listBox3.Items.Add(MailToAddress);
    txtFCount.Text=listBox3.Items.Count.ToString();
    txtFReport.Text=MailToAddress + "邮件发送失败" +ee.ToString();
    string strEmailFailed=MailToAddress;
    string strEmailFailedFileName=Application.StartupPath+"//EmailFailed.txt";
    if (File.Exists(strEmailFailedFileName))
    {
     StreamWriter strwriterobj=File.AppendText(strEmailFailedFileName);
     strwriterobj.WriteLine(strEmailFailed);
     strwriterobj.Close();
    }
    else
    {
     StreamWriter strwriterobj=File.CreateText(strEmailFailedFileName);
     strwriterobj.WriteLine(strEmailFailed);
     strwriterobj.Close();
    }
    
    
   }
   txtFinish.Text=(int.Parse(txtFinish.Text)+1).ToString();   
  }
  /// <summary>
  /// 密送邮件
  /// </summary>
  /// <param name="MailToAddress"></param>
  /// <param name="MailBccAddress"></param>
  private void SendTwoEmail(string MailToAddress,string MailBccAddress)
  {
 
   string MailFromAddress=txtEmail.Text;   
   string MailTitle=txtEmailTitle.Text;
   string MailContent=txtEmailContent.Text;
   string MailSmtpServer=textBoxsmtp.Text; 
   int TempMailServer=MailFromAddress.IndexOf("@",0);
   string MailUserName=MailFromAddress.Substring(0,TempMailServer).Trim();
   string MailPassword=txtPassword.Text.Trim();   
   //string MailDdomain=MailFromAddress.Substring((TempMailServer+1),(MailFromAddress.Length-TempMailServer-1));
   try
   { 
    //实例化MailMessage对象
    System.Web.Mail.MailMessage mail=new System.Web.Mail.MailMessage(); 
    //定义邮件的发送地址
    mail.From=MailFromAddress;
    //定义邮件的接收地址
    //设置以分号分隔的收件人电子邮件地址列表
    mail.To=MailToAddress;
    //定义邮件的主题
    mail.Subject=MailTitle;
    //设置电子邮件正文的内容类型
    //在这里我们以HTML的格式发送
    mail.BodyFormat=System.Web.Mail.MailFormat.Html;
    //设置电子邮件的正文
    mail.Body=MailContent;
    //mail.BodyEncoding.EncodingName.
    //定义邮件的暗送地址
    //设置以分号分隔的电子邮件地址列表
    mail.Bcc=MailBccAddress;
 
    //定义邮件的抄送地址 .抄送会显示在收件人列表中
    //设置以分号分隔的电子邮件地址列表
    //mail.Cc="xqf222@hotmail.com";
 
    //SMTP服务器
    //许多SMTP服务器都需要身份验证
    System.Web.Mail.SmtpMail.SmtpServer=MailSmtpServer;
 
    //验证
    mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 
    //登陆名
    mail.Fields.Add("<A href="http://schemas.microsoft.com/cdo/configuration/sendusername",MailUserName">http://schemas.microsoft.com/cdo/configuration/sendusername",MailUserName); 
    //登陆密码
    mail.Fields.Add("<A href="http://schemas.microsoft.com/cdo/configuration/sendpassword",MailPassword">http://schemas.microsoft.com/cdo/configuration/sendpassword",MailPassword);
 
    //发送
    System.Web.Mail.SmtpMail.Send(mail);
    listBox2.Items.Add(MailToAddress);
    listBox2.Items.Add(MailBccAddress);
    txtSCount.Text=listBox2.Items.Count.ToString();
   
    string strEmailSuccessFileName=Application.StartupPath+"//EmailSuccess.txt";
 
    if (File.Exists(strEmailSuccessFileName))
    {
     StreamWriter strwriterobj=File.AppendText(strEmailSuccessFileName);     
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.WriteLine(MailBccAddress);
     strwriterobj.Close();    
    }
    else
    {
     StreamWriter strwriterobj=File.CreateText(strEmailSuccessFileName);  
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.WriteLine(MailBccAddress);
     strwriterobj.Close();
    }   

   }
   catch(Exception ee)
   {
    listBox3.Items.Add(MailToAddress);
    listBox3.Items.Add(MailBccAddress);
    txtFCount.Text=listBox3.Items.Count.ToString();
    txtFReport.Text= "邮件发送失败地址:" +"/r/n"+ MailToAddress+"/r/n" +MailBccAddress+ "/r/n"  +ee.ToString();   
    string strEmailFailedFileName=Application.StartupPath+"//EmailFailed.txt";
    if (File.Exists(strEmailFailedFileName))
    {
     StreamWriter strwriterobj=File.AppendText(strEmailFailedFileName);
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.WriteLine(MailBccAddress);
     strwriterobj.Close();
    }
    else
    {
     StreamWriter strwriterobj=File.CreateText(strEmailFailedFileName);
     strwriterobj.WriteLine(MailToAddress);
     strwriterobj.WriteLine(MailBccAddress);
     strwriterobj.Close();
    }
    
    
   }
   txtFinish.Text=(int.Parse(txtFinish.Text)+1).ToString();   
  } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疾风铸境

提供工作中碰到的和研究过的技术

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值