控制器:
[AllowAnonymous] [HttpPost] public JsonResult SendEmail(QuoEmail email) { using (var bll = new QuotationsBLL()) { bool isPost = true; string check = string.Empty;
try { email.Body = Server.HtmlDecode(email.Body); if (email.QuoType == "OP" && email.QuoIDs != null) { var quos = bll.QueryQuotations(p => email.QuoIDs.Contains(p.QuotationID)); email.Attachments = new List<string>(); foreach (var quo in quos) { string path = Server.MapPath(quo.ZipFile); email.Attachments.Add(path); } }
//收件人 var strTo = email.To; string[] strToArray = strTo.Split(';'); for (int i = 0; i < strToArray.Length; i++) { if (strToArray[i] != "") { isPost = Mail.SendMail.SendMailForQuotations(email.From, strToArray[i], email.Title, true, email.Body, "smtp-mail.outlook.com", email.From, email.Password, email.Attachments, check); if (!isPost) return Json(new { isPost = false, msg = "发送邮件失败!" }, JsonRequestBehavior.AllowGet); } }
if (isPost) { var sendBy = UserID; if (email.QuoType == "OP") { var quos = bll.QueryQuotations(p => email.QuoIDs.Contains(p.QuotationID)); foreach (var quo in quos) { quo.SentBy = sendBy; quo.IsSent = true; quo.SentTime = DateTime.Now; }
bll.Db.SaveChanges(); }
//在输入密码的时候自动保存密码到Cookie HttpCookie _userInfoCookies = new HttpCookie("userinfo"); _userInfoCookies["UserName"] = email.From; _userInfoCookies["PassWord"] = email.Password; Response.Cookies.Add(_userInfoCookies); }
return Json(new { isPost = isPost, msg = isPost ? "邮件已发送!" : check, }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { isPost = false, msg = "发送邮件失败," + ex.Message }, JsonRequestBehavior.AllowGet); } } }
后台:
/// <summary> /// 发送邮件 /// </summary> /// <param name="from">发送人邮件地址</param> /// <param name="to">接收人邮件地址</param> /// <param name="subject">邮件主题</param> /// <param name="isBodyHtml">是否是Html</param> /// <param name="body">邮件体</param> /// <param name="smtpHost">SMTP服务器地址,例如:smtp.163.com</param> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <param name="list">附件路径(包含多个)</param> /// <returns>是否成功</returns> public static bool SendMailForQuotations(string from, string to, string subject, bool isBodyHtml, string body, string smtpHost, string userName, string password, List<string> list, string msg) { bool isSuccess = true; try { MailMessage mm = new MailMessage(); mm.From = new MailAddress(from); mm.To.Add(new MailAddress(to.Trim())); mm.Subject = subject; mm.IsBodyHtml = isBodyHtml; mm.Body = body; if (list != null) { System.Net.Mail.Attachment att = null; foreach (var item in list) { att = new Attachment(item); mm.Attachments.Add(att); } }
//sc.UseDefaultCredentials = true;//winform中不受影响,asp.net中,false表示不发送身份验证信息 //smtpClient.EnableSsl = true;//如果服务器不支持ssl则报,服务器不支持安全连接 错误 //sc.DeliveryMethod = SmtpDeliveryMethod.Network; //SmtpClient sc = new SmtpClient(smtpHost, 587); //sc.Credentials = new System.Net.NetworkCredential(userName, password); //sc.EnableSsl = true; //sc.Send(mm);
SmtpClient client = new SmtpClient(); client.Host = smtpHost; client.Port = 587; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(userName, password); mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; client.Send(mm); } catch (Exception ex) { isSuccess = false; msg = ex.Message; }
return isSuccess; }
提示:
client.EnableSsl = true; //微软也支持;
|
请发表评论