国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現發送郵件的方法

2020-01-24 00:28:19
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴义市| 卢氏县| 修文县| 临泽县| 虹口区| 夏津县| 潼关县| 漳平市| 新野县| 砀山县| 溧水县| 礼泉县| 平安县| 轮台县| 大邑县| 大关县| 建昌县| 丰镇市| 游戏| 桐城市| 定远县| 营口市| 沿河| 大埔县| 方山县| 东乡县| 融水| 吴江市| 且末县| 南投市| 武乡县| 乌兰浩特市| 宜兰县| 五原县| 高雄市| 周至县| 万载县| 孝感市| 瓦房店市| 阳春市| 前郭尔|