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

首頁(yè) > 編程 > C# > 正文

C#發(fā)送郵箱實(shí)現(xiàn)代碼

2019-10-29 21:16:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

之前自己從來(lái)沒(méi)有做過(guò)發(fā)送郵箱的功能,前段時(shí)間項(xiàng)目需要,在找了很多帖子之后,終于實(shí)現(xiàn)了。

之后有整理了一下,寫(xiě)了一個(gè)類(lèi)。直接給類(lèi)傳遞信息,就可以發(fā)送了。

這里還需要說(shuō)明的是,發(fā)送郵箱需要開(kāi)通POP3/SMTP服務(wù),否則QQ郵箱,網(wǎng)易郵箱等會(huì)報(bào)錯(cuò)。接收的郵箱就不用開(kāi)通啦,開(kāi)通方法百度一下就知道啦。

public static class EmailHelper  {    /// <summary>    /// 發(fā)送郵件    /// </summary>    /// <param name="subject">郵件主題</param>    /// <param name="msg">郵件內(nèi)容</param>    /// <param name="filePath">附件地址,如果不添加附件傳null或""</param>    /// <param name="senderEmail">發(fā)送人郵箱地址</param>    /// <param name="senderPwd">發(fā)送人郵箱密碼</param>    /// <param name="recipientEmail">接收人郵箱</param>    public static void SendMail(string subject, string msg, string filePath, string senderEmail, string senderPwd, params string[] recipientEmail)    {      if (!CheckIsNotEmptyOrNull(subject, msg, senderEmail, senderPwd) || recipientEmail == null || recipientEmail.Length == 0)      {        throw new Exception("輸入信息無(wú)效");      }      try      {        string[] sendFromUser = senderEmail.Split('@');        //構(gòu)造一個(gè)Email的Message對(duì)象        MailMessage message = new MailMessage();        //確定smtp服務(wù)器地址。實(shí)例化一個(gè)Smtp客戶端        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp." + sendFromUser[1]);        //構(gòu)造發(fā)件人地址對(duì)象        message.From = new MailAddress(senderEmail, sendFromUser[0], Encoding.UTF8);        //構(gòu)造收件人地址對(duì)象        foreach (string userName in recipientEmail)        {          message.To.Add(new MailAddress(userName, userName.Split('@')[0], Encoding.UTF8));        }        if (!string.IsNullOrEmpty(filePath))        {          Attachment attach = 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);        }        //添加郵件主題和內(nèi)容        message.Subject = subject;        message.SubjectEncoding = Encoding.UTF8;        message.Body = msg;        message.BodyEncoding = Encoding.UTF8;        //設(shè)置郵件的信息        client.DeliveryMethod = SmtpDeliveryMethod.Network;        message.BodyEncoding = System.Text.Encoding.UTF8;        message.IsBodyHtml = false;        //如果服務(wù)器支持安全連接,則將安全連接設(shè)為true。        //gmail,qq支持,163不支持        switch (sendFromUser[1])        {          case "gmail.com":          case "qq.com":            client.EnableSsl = true;            break;          default:            client.EnableSsl = false;            break;        }        //設(shè)置用戶名和密碼。        client.UseDefaultCredentials = false;        //用戶登陸信息        NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd);        client.Credentials = myCredentials;        //發(fā)送郵件        client.Send(message);      }      catch (Exception ex)      {        throw (ex);      }    }    /// <summary>    /// 驗(yàn)證所有傳入字符串不能為空或null    /// </summary>    /// <param name="ps">參數(shù)列表</param>    /// <returns>都不為空或null返回true,否則返回false</returns>    public static bool CheckIsNotEmptyOrNull(params string[] ps)    {      if (ps != null)      {        foreach (string item in ps)        {          if (string.IsNullOrEmpty(item)) return false;        }        return true;      }      return false;    }  }

直接調(diào)用方法,傳遞需要發(fā)送的信息,就可以發(fā)送郵箱了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 迁西县| 宣城市| 大方县| 繁峙县| 株洲市| 嵊泗县| 岑溪市| 嘉祥县| 临朐县| 罗源县| 中江县| 咸丰县| 莱芜市| 二连浩特市| 临猗县| 青冈县| 龙州县| 福安市| 奈曼旗| 洛浦县| 克拉玛依市| 昌都县| 南投市| 曲水县| 富顺县| 临潭县| 柯坪县| 仙桃市| 韶关市| 彭泽县| 嘉黎县| 新昌县| 武汉市| 阿克陶县| 固原市| 凉山| 普宁市| 龙游县| 清水县| 平遥县| 晋江市|