码迷,mamicode.com
首页 > 其他好文 > 详细

发送邮件方法

时间:2015-06-15 16:10:40      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

    //外发
            function Email() {
                var box = "";
                var ids = "";
                if ($(":checkbox[name=‘delTrain‘]:checked").size() == 0) {
                    alert("您没有选择要外发的数据!");
                    return;
                }
                if (!confirm(‘您真的要外发所选应用吗?‘)) {
                    return false;
                }
                var c = 0;
                $("input[name=‘delTrain‘]:checked").each(function () {
                    c++;
                    box = $(this);
                    ids = $(this).attr(‘value‘);
                });
                if (c == "1") {
                    dialog.open({ id: "window_waifa", title: "外发", width: 920, height: 500, url: top.rootdir + ‘/Inf_Train/Email/‘ + ids + "?" + query, openerid: iframeid });
                }
                else {
                    alert(‘外发只能选择一条数据!!!‘)
                    return false;
                }

 

 

     

/// <summary> /// 外发邮件 /// </summary> /// <returns></returns> public JsonResult EmailSend() { JsonResult result = new JsonResult(); GetPubParameter(); var ID = Request["ID"]; var toMail = Request["toMail"]; var ccMail = Request["ccMail"];
var NoticeTitleMeal = Request["NoticeTitleMeal"]; var NoticeCount = ""; Inf_Train model = new Inf_Train(); model = Bll.GetModel(ID); if (model != null) { NoticeCount = model.NoticeCount; } #region 获取附件地址 DataTable dt = annexBll.GetModel("ID =‘" + ID + "‘ and Invalid!=‘1‘"); string[] n = null; if (dt.Rows.Count > 0) { n = new string[dt.Rows.Count]; for (int i = 0; i < n.Length; i++) { if (dt.Rows[i]["AnnexPath"].ToString() != null || dt.Rows[i]["AnnexPath"].ToString() != "") { n[i] = Server.MapPath(dt.Rows[i]["AnnexPath"].ToString()); } } } #endregion //声明一个可以用SmtpClient发送的邮件 MailMessage mail = new MailMessage(); //设置邮件的主题 mail.Subject = "您有新留言"; StringBuilder content = new StringBuilder(); content.Append("主题:").Append(NoticeTitleMeal).Append("<br / >"); content.Append("内容:").Append(NoticeCount).Append("<br / >"); //声明外发邮件对象 MailHelper mailHelper = new MailHelper(); //调用外发邮件方法 bool s = MailHelper.SendEmail(toMail.Replace("", ","), ccMail.Replace("", ","), "", "", NoticeTitleMeal, content.ToString(), n); if (s) { result.Data = new { success = true }; MailHelper.SendEmailLog(Train_ID, "", NoticeTitleMeal, toMail.Replace("", ","), ccMail.Replace("", ","), emailFrom, content.ToString(), n, ""); } else { result.Data = new { success = false }; } return result; }

  <add key="SmtpServer" value="XXXXXX.XXXXXX.com"/>
    <add key="UserName" value="......@.......com"/>
    <add key="Pwd" value="......"/>
    <add key="AuthorName" value="......"/>

 static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
        static readonly string emailFrom = System.Configuration.ConfigurationManager.AppSettings["UserName"];
        static readonly string password = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
        static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
/// <summary> /// 发送邮件 /// </summary> /// <param name="mailTolist">要发送的邮箱(多个以逗号分开 not null)</param> /// <param name="Cclist">抄送的邮箱(多个以逗号分开 allow null)</param> /// <param name="RepalyTo">回复的邮箱(多个以逗号分开 allow null)</param> /// <param name="Bcclist">密送的邮箱(多个以逗号分开 allow null)</param> /// <param name="mailSubject">邮箱主题</param> /// <param name="mailContent">邮箱内容</param> /// <param name="AttachmentUrl">附件URL(前加@或转义、控件.PostedFile.FileName)(数组)</param> /// <returns>返回发送邮箱的结果</returns> public static bool SendEmail(string mailTolist, string Cclist, string RepalyTo, string Bcclist, string mailSubject, string mailContent, string[] AttachmentUrl) { // 邮件服务设置 SmtpClient smtpClient = new SmtpClient(); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 smtpClient.Host = smtpServer; //指定SMTP服务器 smtpClient.Credentials = new System.Net.NetworkCredential(emailFrom, password);//用户名和密码 MailMessage mailMessage = new MailMessage(emailFrom, mailTolist);//添加一组收件人 // 发送邮件设置 if (!string.IsNullOrEmpty(Cclist)) { mailMessage.CC.Add(Cclist);//添加一组抄送 } if (!string.IsNullOrEmpty(RepalyTo)) { mailMessage.ReplyToList.Add(RepalyTo);//添加一组回复 } if (!string.IsNullOrEmpty(Bcclist)) { mailMessage.Bcc.Add(Bcclist);//添加一组密送 } if (AttachmentUrl!=null) { if (AttachmentUrl.Length >= 1) { if (!(AttachmentUrl.Length == 1 && AttachmentUrl[0] == "")) { for (int i = 1; i <= AttachmentUrl.Length; i++) { //判断文件是否存在 if (File.Exists(AttachmentUrl[i - 1].ToString())) { Attachment myfiles = new Attachment(AttachmentUrl[i - 1]);//上传附件 mailMessage.Attachments.Add(myfiles); } } } } } mailMessage.Subject = mailSubject;// "测试主题";//主题 mailMessage.Body = mailContent;//邮件内容 mailMessage.BodyEncoding = Encoding.UTF8;//正文编码 mailMessage.IsBodyHtml = true;//设置为HTML格式 mailMessage.Priority = MailPriority.Low;//优先级 string EndInformation = string.Empty; try { smtpClient.Send(mailMessage); // 发送邮件 EndInformation = "发送成功"; return true; } catch (SmtpException ex) { EndInformation = "发送失败:" + ex.ToString(); return false; } }

 

发送邮件方法

标签:

原文地址:http://www.cnblogs.com/zmmlczy/p/4577490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!