zoukankan      html  css  js  c++  java
  • ASP.NET MVC 邮件发送的功能(微软邮箱发送)。

    控制器:

    [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;  //微软也支持;

  • 相关阅读:
    递归函数思想理解
    关于C++11 模板中的 using
    DES与3DES
    c++ 内存
    单词替换程序demo
    GF(256)下数的乘法 转化为矩阵乘法
    码片速率的含义
    转载 WCDMA中码片速率、符号速率、bit速率 WCDMA常用概念
    LTE 到GSM 的CCO过程是怎样的【转载自360】
    转载自搜狐科技【技术那些事儿】LTE网络中的用户数据库HSS与传统2G/3G的HLR有何区别?能否融合组网?
  • 原文地址:https://www.cnblogs.com/Engels-Zeng/p/7993714.html
Copyright © 2011-2022 走看看