• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETMVC邮件发送的功能(微软邮箱发送)。

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

控制器:

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.netAjax中的脚本错误:Sys未定义的解决方法二发布时间:2022-07-10
下一篇:
ASP.NETCorehtml生成pdf转发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap