zoukankan      html  css  js  c++  java
  • 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();   
      } 

  • 相关阅读:
    【JavaP6大纲】Java基础篇:为什么jdk8以后HashMap会使用红黑树优化?
    【JavaP6大纲】Java基础篇:HashMap加载因子为什么是0.75?
    【JavaP6大纲】Zookeeper篇:选举机制
    就是要幸福(1)严于律人
    天真的童年
    闲言碎语话心得垃圾工作
    镜花水月
    就是要幸福(3)言行自由
    五年
    爸爸我给你捂捂手
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306752.html
Copyright © 2011-2022 走看看