C#中發送郵件的實現方法詳解

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了C#實現發送郵件的方法,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文執行個體為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下


#region 發送郵件部分    private static String fromMail = "1111@126.com";  ///郵箱名稱    private static String mailPwd = "111111";     ///密碼    private static string toMail = "2222@163.com";  ///郵箱伺服器    private static string fileStr;//當前圖片路徑,在添加附件時用    /// <summary>    /// 發送郵件    /// </summary>    /// <param name="fileUrl">附件地址,以~分</param>    /// <param name="screen">是否發送截屏</param>    /// <returns></returns>    public static string SendMail(string fileUrl, string screen)    {           MailAddress from = new MailAddress(fromMail);      MailAddress to = new MailAddress(toMail);      MailMessage message = new MailMessage(from, to);      message.Subject = "M郵件 " +11111;//主題      message.SubjectEncoding = System.Text.Encoding.UTF8;      Attachment attachFile = new Attachment(fileStr);      if (screen == "True")        message.Attachments.Add(attachFile);      string[] files = fileUrl.Split('~');      for (int i = 0; i < files.Length; i++)      {        if (File.Exists(files[i]))        {          Attachment attachFile1 = new Attachment(fileUrl);          message.Attachments.Add(attachFile1);        }      }      try      {        SmtpClient client = new SmtpClient("smtp." + from.Host);        SendMail(client, from, mailPwd, to, message);               return "發送郵件成功!包含附件:" + fileUrl + " 含?" + screen + "  " + DateTime.Now.ToString();      }      catch (SmtpException ex)      {        //如果錯誤原因是沒有找到伺服器,則嘗試不加smtp.首碼的伺服器        if (ex.StatusCode == SmtpStatusCode.GeneralFailure)        {          try          {            //有些郵件伺服器不加smtp.首碼            SmtpClient client = new SmtpClient(from.Host);            SendMail(client, from, mailPwd, to, message);                      return "發送郵件成功!包含附件:" + fileUrl + " 含?" + screen + "  " + DateTime.Now.ToString();          }          catch (SmtpException err)          {                       return "發送郵件失敗!" + err.Message + "  " + DateTime.Now.ToString();          }        }        else        {                   return "發送郵件失敗!" + ex.Message + "  " + DateTime.Now.ToString();        }      }    }    //根據指定的參數發送郵件    private static void SendMail(SmtpClient client, MailAddress from, string password,       MailAddress to, MailMessage message)    {      //不使用預設憑證,注意此句必須放在client.Credentials的上面      client.UseDefaultCredentials = false;      //指定使用者名稱、密碼      client.Credentials = new NetworkCredential(from.Address, password);      //郵件通過網路發送到伺服器      client.DeliveryMethod = SmtpDeliveryMethod.Network;      try      {        client.Send(message);      }      catch      {        throw;      }      finally      {        //及時釋放佔用的資源        message.Dispose();      }    }    #endregion
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.