发送邮件方法

    //外发
            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="[email protected]"/>    <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;
            }
        }
时间: 2024-10-12 07:18:15

发送邮件方法的相关文章

C# .NET发送邮件方法

一.发送邮件方法 1 ///<summary> 2 /// 发送邮件方法 3 ///</summary> 4 ///<param name="mailTo">收件人邮箱</param> 5 ///<param name="mailSubject">邮件标题</param> 6 ///<param name="mailContent">邮件内容</param&

python发送邮件方法总结

python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点.     一.相关模块介绍 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍:    1.smtplib模块 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执

PHPMailer发送邮件方法

/** * * 测试邮件发送s * @param 服务器 $Host * @param 端口 $Port * @param 昵称 $Fromname * @param 身份验证用户名 $Username * @param 身份验证密码 $Password * @param 发送人邮件地址 $From * @param 接收人邮件地址 $Address * @param 邮件标题 $Title * @param 邮件正文 $Message * @param 附件 $Attachment */ fu

Django发送邮件方法

在Django中将渲染后的模板进行邮件发送,可以使用send_email方法 首先在settings.py中添加如下配置 # 邮件配置SSL加密方式 EMAIL_HOST = 'smtp.qq.com' EMAIL_PORT = 465 # 使用SSL加密方式端口为465 EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'pwd' EMAIL_USE_SSL = True # 使用SSL加密方式 EMAIL_FROM = '

python发送邮件方法

1.普通文本邮件 #!/usr/bin/env python # -*- coding:utf-8 -*- import smtplib from email.mime.text import MIMEText mail_user="[email protected]" #发送邮件的邮箱 mail_pass="xxxxxxx" #密码,口令 mailto_list="[email protected]" #接受邮件的邮箱 mail_host=&q

亲测可用的发送邮件方法分享

//发送邮件代码 public static void Sends(string email, string formto, string content, string body, string upass) { string name = "[email protected]"; string smtp = "smtp.exmail.sina.com"; SmtpClient _smtpClient = new SmtpClient(); _smtpClient

php发送邮件方法-亲测可用,email.class.php过期解决办法

php虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,使用起来也是非常简单!使用PHPMailer发送邮件: <?php header("content-type:text/html;charset=utf-8"); ini_set("magic_quotes_runtime",0); require 'class.phpmailer.php'; try { $mail = new PHPMailer(true); $ma

C# 发送邮件方法

private void toemail() { try { //邮件发送类? MailMessage mail = new MailMessage(); //是谁发送的邮件? mail.From = new MailAddress("[email protected]", "zx"); //发送给谁? mail.To.Add("[email protected]"); //标题? mail.Subject = "test";

linux发送邮件方法

操作系统:centos6.5 需要软件:postfix  默认安装有 如果没有 yum install postfix即可 mutt   yum install mutt 安装后编辑 vi /etc/muttrc defaultsaccount soomenghost smtp.126.comfrom [email protected]auth loginport 25tls offuser [email protected]password yourpasswdaccount default