C#發送郵箱實現代碼 (轉自CFEI.NET)

發送郵箱需要開通POP3/SMTP服務,否則QQ郵箱,網易郵箱等會報錯。

public static class EmailHelper

  {

    ///<summary>

    ///發送郵件

    ///</summary>

    ///<paramname="subject">郵件主題</param>

    ///<paramname="msg">郵件內容</param>

    ///<paramname="filePath">附件地址,如果不添加附件傳null或""</param>

    ///<paramname="senderEmail">發送人郵箱地址</param>

    ///<paramname="senderPwd">發送人郵箱密碼</param>

    ///<paramname="recipientEmail">接收人郵箱</param>

   public static void SendMail(string subject, string msg, stringfilePath, string senderEmail, string senderPwd, params string[]recipientEmail)

   {

     if (!CheckIsNotEmptyOrNull(subject, msg,senderEmail, senderPwd) || recipientEmail == null ||recipientEmail.Length == 0)

     {

       throw newException("輸入信息無效");

     }

     try

     {

       string[] sendFromUser =senderEmail.Split('@');

      //構造一個Email的Message對象

       MailMessage message = newMailMessage();

 

      //確定smtp服務器地址。實例化一個Smtp客戶端

       System.Net.Mail.SmtpClientclient = new System.Net.Mail.SmtpClient("smtp." +sendFromUser[1]);

       //構造發件人地址對象

       message.From = newMailAddress(senderEmail, sendFromUser[0],Encoding.UTF8);

 

       //構造收件人地址對象

       foreach (string userName inrecipientEmail)

       {

        message.To.Add(new MailAddress(userName, userName.Split('@')[0],Encoding.UTF8));

       }

       if(!string.IsNullOrEmpty(filePath))

       {

         Attachmentattach = new Attachment(filePath);

        //得到文件的信息

        ContentDisposition disposition =attach.ContentDisposition;

        disposition.CreationDate =System.IO.File.GetCreationTime(filePath);

        disposition.ModificationDate =System.IO.File.GetLastWriteTime(filePath);

        disposition.ReadDate =System.IO.File.GetLastAccessTime(filePath);

        //向郵件添加附件

        message.Attachments.Add(attach);

       }

 

       //添加郵件主題和內容

       message.Subject =subject;

       message.SubjectEncoding =Encoding.UTF8;

       message.Body =msg;

       message.BodyEncoding =Encoding.UTF8;

       //設置郵件的信息

       client.DeliveryMethod =SmtpDeliveryMethod.Network;

       message.BodyEncoding =System.Text.Encoding.UTF8;

       message.IsBodyHtml =false;

 

      //如果服務器支持安全連接,則將安全連接設爲true。

      //gmail,qq支持,163不支持

       switch(sendFromUser[1])

       {

         case"gmail.com":

         case"qq.com":

          client.EnableSsl = true;

          break;

        default:

          client.EnableSsl = false;

          break;

       }

       //設置用戶名和密碼。

       client.UseDefaultCredentials= false;

       //用戶登陸信息

       NetworkCredentialmyCredentials = new NetworkCredential(senderEmail,senderPwd);

       client.Credentials =myCredentials;

       //發送郵件

      client.Send(message);

     }

     catch (Exception ex)

     {

       throw (ex);

     }

   }

    ///<summary>

    ///驗證所有傳入字符串不能爲空或null

    ///</summary>

    ///<paramname="ps">參數列表</param>

    ///<returns>都不爲空或null返回true,否則返回false</returns>

   public static bool CheckIsNotEmptyOrNull(params string[]ps)

   {

     if (ps != null)

     {

       foreach (string item inps)

       {

         if(string.IsNullOrEmpty(item)) return false;

       }

       return true;

     }

     return false;

   }

  }

發佈了53 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章