C#實現(xiàn)發(fā)送郵件的方法
更新時間:2017年09月13日 13:13:54 作者:高軟玩家
這篇文章主要為大家詳細介紹了C#實現(xiàn)發(fā)送郵件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下
#region 發(fā)送郵件部分 private static String fromMail = "1111@126.com"; ///郵箱名稱 private static String mailPwd = "111111"; ///密碼 private static string toMail = "2222@163.com"; ///郵箱服務器 private static string fileStr;//當前圖片路徑,在添加附件時用 /// <summary> /// 發(fā)送郵件 /// </summary> /// <param name="fileUrl">附件地址,以~分</param> /// <param name="screen">是否發(fā)送截屏</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 "發(fā)送郵件成功!包含附件:" + 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 "發(fā)送郵件成功!包含附件:" + fileUrl + " 含截圖?" + screen + " " + DateTime.Now.ToString(); } catch (SmtpException err) { return "發(fā)送郵件失??!" + err.Message + " " + DateTime.Now.ToString(); } } else { return "發(fā)送郵件失敗!" + ex.Message + " " + DateTime.Now.ToString(); } } } //根據(jù)指定的參數(shù)發(fā)送郵件 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); //郵件通過網(wǎng)絡發(fā)送到服務器 client.DeliveryMethod = SmtpDeliveryMethod.Network; try { client.Send(message); } catch { throw; } finally { //及時釋放占用的資源 message.Dispose(); } } #endregion
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
NumberToUpper數(shù)字轉(zhuǎn)中文詳解
本文介紹NumberToUpper數(shù)字轉(zhuǎn)中文的方法,大家參考使用吧2013-12-12C#通過System.CommandLine快速生成支持命令行的應用程序
這篇文章介紹了C#通過System.CommandLine快速生成支持命令行應用程序的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07