平台之大勢何人能擋? 帶着你的Net飛奔吧!
http://www.cnblogs.com/dunitian/p/4822808.html
郵箱系列:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/07.Email
1.QQ郵箱:
他生成的是:http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=7oKBmoqAmq6fn8CNgYM
后來我把后面加密字符串換成明文的郵箱==》發現一樣用 :http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=1054186320@qq.com
代碼案例:
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> 官方生成代碼:(其實就是把你的郵件加了下密)<br /><br /> <a target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=7oKBmoqAmq6fn8CNgYM" style="text-decoration:none;"> <img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_12.png" /> </a> <br /><br /> 逆天通用土方法:(郵件可以任意替換)<br /><br /> <a target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=1054186320@qq.com">郵我</a> </body> </html>
效果:
訂閱系列可以參考:http://www.cnblogs.com/dunitian/p/4554084.html
——————————————————————————————————————————————————————————————————————————————————————————————
郵件發送的前提設置:
QQ郵箱設置
授權碼生成:
2.郵件案例:
簡單熟悉郵件系列:點我就看基礎案例
代碼示例:
#region 附件路徑 /// <summary> /// 附件路徑 /// </summary> public static List<string> filePathList = new List<string>(); #endregion #region 文件上傳 /// <summary> /// LoTUploader-文件上傳 /// </summary> /// <returns></returns> public JsonResult FileUpload(System.Web.HttpPostedFileBase file) { if (file == null) { return Json(new { status = false, msg = "文件提交失敗" }); } if (file.ContentLength > 10485760) { return Json(new { status = false, msg = "文件10M以內" }); } string filterStr = ".gif,.jpg,.jpeg,.bmp,.png|.rar,.7z,.zip"; string fileExt = Path.GetExtension(file.FileName).ToLower(); if (!filterStr.Contains(fileExt)) { return Json(new { status = false, msg = "請上傳圖片或壓縮包" }); } //防止黑客惡意繞過,判斷下文件頭文件 if (!file.InputStream.CheckingExt("7173", "255216", "6677", "13780", "8297", "55122", "8075")) { //todo:一次危險記錄 return Json(new { status = false, msg = "請上傳圖片或壓縮包" }); } //todo: md5判斷一下文件是否已經上傳過,如果已經上傳直接返回 return Json(new { status = true, msg = sqlPath }); string path = string.Format("{0}/{1}", "/lotFiles", DateTime.Now.ToString("yyyy-MM-dd")); string fileName = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), fileExt); string sqlPath = string.Format("{0}/{1}", path, fileName); string dirPath = Request.MapPath(path); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } try { file.SaveAs(Path.Combine(dirPath, fileName)); file.InputStream.Dispose(); filePathList.Add(Path.Combine(dirPath, fileName)); } catch { return Json(new { status = false, msg = "文件保存失敗" }); } return Json(new { status = true, msg = sqlPath }); } #endregion #region 發郵件 /// <summary> /// 發郵件 /// </summary> /// <param name="model"></param> /// <returns></returns> public async Task<JsonResult> SendMsg(MailModel model) { var obj = new AjaxOption<object>(); #region 校驗系列 if (model == null) { obj.Msg = "內容不能為空"; } if (string.IsNullOrWhiteSpace(model.MailSubject)) { obj.Msg = "郵件主題不能為空"; } if (string.IsNullOrWhiteSpace(model.MailContent)) { obj.Msg = "郵件內容不能為空"; } #region 收件人郵箱 if (model.MailToList != null) { foreach (var item in model.MailToList) { if (!item.IsEmail()) { model.MailToList.Remove(item); } } } else { obj.Msg = "收件人郵箱不能為空"; } //這個一定要加 if (model.MailToList.Count == 0) { obj.Msg = "收件人郵箱不能為空"; } #endregion if (model.MailCCList.ExistsData()) { foreach (var item in model.MailCCList) { if (!item.IsEmail()) { model.MailCCList.Remove(item); } } } #endregion //內容解碼 model.MailContent = System.Web.HttpUtility.UrlDecode(model.MailContent); //添加附件 if (filePathList.ExistsData()) { model.AttachmentList=filePathList; } if (obj.Msg.IsNullOrWhiteSpace()) obj.Status = await EmailHelper.SendAsync(model); return Json(obj); } #endregion
3.項目應用:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/07.Email/2.EmailAPI
其實項目里面基本上是不用他附件功能的,比如你注冊的時候發一個郵件給你來激活,你敏感操作的時候給你一個提示(比如異地登陸或者修改密碼)
簡單封裝了一個api,一般每個項目里面都有這個發郵件的功能,很多公司把這些諸如上傳,發郵件,短信通知的功能都封裝成api,用的時候調用一下就可以了
效果圖就不貼了,和上面差不多,就是沒上傳附件了
#region 發郵件 /// <summary> /// 發郵件 /// </summary> /// <param name="model"></param> /// <returns></returns> [CrossSite] public async Task<string> Post([FromUri]MailModel model) { var obj = new AjaxOption<object>(); #region 校驗系列 if (model == null) { obj.Msg = "內容不能為空"; } if (string.IsNullOrWhiteSpace(model.MailSubject)) { obj.Msg = "郵件主題不能為空"; } if (string.IsNullOrWhiteSpace(model.MailContent)) { obj.Msg = "郵件內容不能為空"; } #region 收件人郵箱 if (model.MailToList != null) { foreach (var item in model.MailToList) { if (!item.IsEmail()) { model.MailToList.Remove(item); } } } else { obj.Msg = "收件人郵箱不能為空"; } //這個一定要加 if (model.MailToList.Count == 0) { obj.Msg = "收件人郵箱不能為空"; } #endregion if (model.MailCCList.ExistsData()) { foreach (var item in model.MailCCList) { if (!item.IsEmail()) { model.MailCCList.Remove(item); } } } #endregion //內容解碼 model.MailContent = System.Web.HttpUtility.UrlDecode(model.MailContent); if (obj.Msg.IsNullOrWhiteSpace()) obj.Status = await EmailHelper.SendAsync(model); return obj.ObjectToJson(); } #endregion
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>郵件系列</title> <meta charset="utf-8" /> <link href="//cdn.bootcss.com/wangeditor/2.1.10/css/wangEditor.min.css" rel="stylesheet" /> <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="rows"> <div class="form-horizontal"> <!--收件人郵箱--> <div class="form-group"> <br /> <label class="col-sm-2 control-label">收件郵箱:</label> <div class="col-sm-6"> <input type="text" class="form-control" id="mailTo" placeholder="請輸入收件人郵箱..."> </div> </div> <!--收件人郵箱--> <div class="form-group"> <br /> <label class="col-sm-2 control-label">抄送郵箱:</label> <div class="col-sm-6"> <input type="text" class="form-control" id="mailCC" placeholder="請輸入抄送人郵箱..."> </div> </div> <!--標題--> <div class="form-group"> <br /> <label class="col-sm-2 control-label">主題名稱:</label> <div class="col-sm-6"> <input type="text" class="form-control" id="mailSubject" placeholder="請輸入郵箱主題名稱..."> </div> </div> <!--編輯器--> <div class="form-group"> <br /> <label class="col-sm-2 control-label">正文內容:</label> <div class="col-sm-6"> <div id="edit" style="min-height:20em"></div> </div> </div> <!--表單提交--> <div class="form-group"> <br /> <label class="col-sm-2 control-label"></label> <div class="col-sm-6"> <button id="btn" class="btn btn-success form-control">發送郵件</button> </div> </div> </div> </div> <div id="msg"></div> <script src="//cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js" async></script> <script src="//cdn.bootcss.com/wangeditor/2.1.10/js/wangEditor.min.js"></script> <script type="text/javascript"> // 為頁面所有的editor配置全局的密鑰 //wangEditor.config.mapAk = 'xxxxxxxxxxxxxx'; wangEditor.config.printLog = false; //阻止輸出log var editor = new wangEditor('edit'); editor.create(); $('#btn').click(function () { // 獲取編輯器區域完整html代碼 var mailTo = $('#mailTo').val(); var mailCC = $('#mailCC').val(); var mailSubject = $('#mailSubject').val(); var mailContent = escape(editor.$txt.html()); if (mailTo.length < 1 || mailCC.length < 1 || mailSubject.length < 1 || mailContent.leng < 1) { $('#msg').html('<h2>除附件外,不能為空!</h2>'); return false; } $.post('/api/email?MailSubject=' + mailSubject + '&MailContent=' + mailContent + '&MailToList=' + mailTo + '&MailCCList=' + mailCC, {}, function (data) { data = JSON.parse(data); if (data.Status) { $('#msg').html('<h2>發送成功!</h2>'); } else { $('#msg').html('<h2>' + data.Msg + '</h2>'); } }) }); </script> </body> </html>
EmailHelper:
using System.Net.Mail; using System.Configuration; using System.Threading.Tasks; using System.Collections.Generic; #region MailModel /// <summary> /// MailModel /// </summary> public class MailModel { /// <summary> /// 郵箱主題 /// </summary> public string MailSubject { get; set; } /// <summary> /// 郵箱內容 /// </summary> public string MailContent { get; set; } /// <summary> /// 收件人郵箱 /// </summary> public List<string> MailToList { get; set; } /// <summary> /// 抄送人郵箱 /// </summary> public List<string> MailCCList { get; set; } /// <summary> /// 附件路徑 /// </summary> public List<string> AttachmentList { get; set; } } #endregion public class EmailHelper { private static string mailFrom = ConfigurationManager.AppSettings["EmailForm"]; //登陸用戶名 private static string mailPass = ConfigurationManager.AppSettings["EmailPass"]; //登陸密碼 private static string mailSmtp = ConfigurationManager.AppSettings["EmailSmtp"]; //SMTP服務器 #region 發送郵件 /// <summary> /// 發送郵件 /// </summary> /// <param name="mailSubject">郵箱主題</param> /// <param name="mailContent">郵箱內容</param> /// <param name="mailTo">收件人郵箱</param> /// <param name="mailCC">抄送人郵箱</param> /// <param name="attachmentsPath">附件路徑</param> /// <returns>返回發送郵箱的結果</returns> public static async Task<bool> SendAsync(MailModel model) { #region 基本校驗(一般都是調用前就驗證了) //if (model == null || string.IsNullOrWhiteSpace(model.MailSubject) || string.IsNullOrWhiteSpace(model.MailContent) || model.MailTo == null) //{ // return false; //} #endregion //郵件服務設置 using (var smtpClient = new SmtpClient()) { smtpClient.Host = mailSmtp; //指定SMTP服務器 smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, mailPass); //用戶名和密碼 using (var mailMsg = new MailMessage()) { //發信人郵箱 mailMsg.From = new MailAddress(mailFrom); //收件人郵箱 foreach (var item in model.MailToList) { mailMsg.To.Add(item); } //抄送人郵箱 if (model.MailCCList != null && model.MailCCList.Count > 0) { foreach (var item in model.MailCCList) { mailMsg.CC.Add(item); } } //附件系列 if (model.AttachmentList != null) { foreach (var item in model.AttachmentList) { try { mailMsg.Attachments.Add(new Attachment(item)); } catch (System.Exception ex) { } } } mailMsg.Subject = model.MailSubject; //主題 mailMsg.Body = model.MailContent; //內容 mailMsg.BodyEncoding = System.Text.Encoding.UTF8; //編碼 mailMsg.IsBodyHtml = true; //HTML格式 try { await smtpClient.SendMailAsync(mailMsg); //發送郵件 return true; } catch (System.Exception ex) { mailMsg.Dispose(); smtpClient.Dispose(); return false; } } } } #endregion }
如果沒有賬號,我提供幾個測試賬號:
mail.host = smtp.163.com
mail.username = php_tester@163.com
mail.password = php1234
mail.smtp.from = php_tester@163.com
mail.smtp.auth = true——————————————————
mail.host=smtp.yeah.net
mail.port=25
mail.smtp.auth=true
mail.smtp.timeout=25000
mail.username=techblog@yeah.net
mail.password=2436chao——————————————————
mail.server.host=smtp.yeah.net
mail.server.post=25
mail.server.validate=true
mail.server.username=zmc330563778
mail.server.password=zmc586858
mail.server.fromaddress=zmc330563778@yeah.net