コード例 #1
0
        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="isSimple">是否只发送一条</param>
        /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param>
        /// <param name="isReuse">是否重用SmtpClient</param>
        private void SendMessage(MailHelper mail, bool isSimple, ArrayList list, Mail_Test.Model.maillist mailinfo, bool autoReleaseSmtp, bool isReuse)
        {
            SmtpClient client = new SmtpClient();

            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl      = true;
            client.Host           = "smtp.qq.com";
            client.Port           = 587;

            // setup Smtp authentication
            System.Net.NetworkCredential credentials =
                new System.Net.NetworkCredential("*****@*****.**", "cjwmmxfkvcnhbgja");
            client.UseDefaultCredentials = false;
            client.Credentials           = credentials;

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress("*****@*****.**");
            msg.To.Add(new MailAddress("*****@*****.**"));
            //msg.Attachment.Add(@"c:\测试.TXT");
            //msg.Attachment.Add(@"c:\screenshot.jpg");
            //msg.Attachment.Add(@"c:\www.shengys.cn.zip");

            if (list != null && list.Count > 0)
            {
                foreach (string filePath in list)
                {
                    msg.Attachments.Add(new Attachment(filePath));
                }
            }

            msg.Subject    = "This is a test Email subject";
            msg.IsBodyHtml = true;
            msg.Body       = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");

            try
            {
                client.Send(msg);
                // lblMsg.Text = "Your message has been successfully sent.";
            }
            catch (Exception ex)
            {
                // lblMsg.ForeColor = Color.Red;
                // lblMsg.Text = "Error occured while sending your message." + ex.Message;
                string x = ex.ToString();
            }


            //SmtpMailClient client = new SmtpMailClient("stmp.163.com", 25, true, Config.TestUserName, Config.TestPassword);
            //client.Timeout = 18000;
            //MailMessage msg = new MailMessage();
            //msg.From = new MailAccount(Config.TestFromAddress, Config.TestFromName);
            //msg.AddTo(new MailAccount(mailinfo.mto, mailinfo.toname));
            //msg.AddCC(new MailAccount(mailinfo.mcc, mailinfo.ccname));
            //msg.MailContent = mailinfo.mailcontent;
            //msg.Subject = mailinfo.title;
            //msg.IsHtml = true;
            //client.Send(msg);
        }
コード例 #2
0
ファイル: maillist.cs プロジェクト: redmantis/SendEmail
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Mail_Test.Model.maillist model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update maillist set ");
            strSql.Append("title=@title,");
            strSql.Append("mailcontent=@mailcontent,");
            strSql.Append("mto=@mto,");
            strSql.Append("mcc=@mcc,");
            strSql.Append("mfrom=@mfrom,");
            strSql.Append("toname=@toname,");
            strSql.Append("ccname=@ccname,");
            strSql.Append("sendtime=@sendtime,");
            strSql.Append("fromname=@fromname,");
            strSql.Append("guid=@guid");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@title",       SqlDbType.VarChar,          100),
                new SqlParameter("@mailcontent", SqlDbType.Text),
                new SqlParameter("@mto",         SqlDbType.VarChar,          100),
                new SqlParameter("@mcc",         SqlDbType.VarChar,          100),
                new SqlParameter("@mfrom",       SqlDbType.VarChar,          100),
                new SqlParameter("@toname",      SqlDbType.VarChar,           50),
                new SqlParameter("@ccname",      SqlDbType.VarChar,           50),
                new SqlParameter("@sendtime",    SqlDbType.DateTime),
                new SqlParameter("@fromname",    SqlDbType.VarChar,           50),
                new SqlParameter("@guid",        SqlDbType.UniqueIdentifier,  16),
                new SqlParameter("@id",          SqlDbType.Int, 4)
            };
            parameters[0].Value  = model.title;
            parameters[1].Value  = model.mailcontent;
            parameters[2].Value  = model.mto;
            parameters[3].Value  = model.mcc;
            parameters[4].Value  = model.mfrom;
            parameters[5].Value  = model.toname;
            parameters[6].Value  = model.ccname;
            parameters[7].Value  = model.sendtime;
            parameters[8].Value  = model.fromname;
            parameters[9].Value  = model.guid;
            parameters[10].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
ファイル: maillist.cs プロジェクト: redmantis/SendEmail
 /// <summary>
 /// 得到一个对象实体
 /// </summary>
 public Mail_Test.Model.maillist DataRowToModel(DataRow row)
 {
     Mail_Test.Model.maillist model = new Mail_Test.Model.maillist();
     if (row != null)
     {
         if (row["id"] != null && row["id"].ToString() != "")
         {
             model.id = int.Parse(row["id"].ToString());
         }
         if (row["title"] != null)
         {
             model.title = row["title"].ToString();
         }
         if (row["mailcontent"] != null)
         {
             model.mailcontent = row["mailcontent"].ToString();
         }
         if (row["mto"] != null)
         {
             model.mto = row["mto"].ToString();
         }
         if (row["mcc"] != null)
         {
             model.mcc = row["mcc"].ToString();
         }
         if (row["mfrom"] != null)
         {
             model.mfrom = row["mfrom"].ToString();
         }
         if (row["toname"] != null)
         {
             model.toname = row["toname"].ToString();
         }
         if (row["ccname"] != null)
         {
             model.ccname = row["ccname"].ToString();
         }
         if (row["sendtime"] != null && row["sendtime"].ToString() != "")
         {
             model.sendtime = DateTime.Parse(row["sendtime"].ToString());
         }
         if (row["fromname"] != null)
         {
             model.fromname = row["fromname"].ToString();
         }
         if (row["guid"] != null && row["guid"].ToString() != "")
         {
             model.guid = new Guid(row["guid"].ToString());
         }
     }
     return(model);
 }
コード例 #4
0
ファイル: maillist.cs プロジェクト: redmantis/SendEmail
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Mail_Test.Model.maillist model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into maillist(");
            strSql.Append("title,mailcontent,mto,mcc,mfrom,toname,ccname,sendtime,fromname,guid)");
            strSql.Append(" values (");
            strSql.Append("@title,@mailcontent,@mto,@mcc,@mfrom,@toname,@ccname,@sendtime,@fromname,@guid)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@title",       SqlDbType.VarChar,          100),
                new SqlParameter("@mailcontent", SqlDbType.Text),
                new SqlParameter("@mto",         SqlDbType.VarChar,          100),
                new SqlParameter("@mcc",         SqlDbType.VarChar,          100),
                new SqlParameter("@mfrom",       SqlDbType.VarChar,          100),
                new SqlParameter("@toname",      SqlDbType.VarChar,           50),
                new SqlParameter("@ccname",      SqlDbType.VarChar,           50),
                new SqlParameter("@sendtime",    SqlDbType.DateTime),
                new SqlParameter("@fromname",    SqlDbType.VarChar,           50),
                new SqlParameter("@guid",        SqlDbType.UniqueIdentifier, 16)
            };
            parameters[0].Value = model.title;
            parameters[1].Value = model.mailcontent;
            parameters[2].Value = model.mto;
            parameters[3].Value = model.mcc;
            parameters[4].Value = model.mfrom;
            parameters[5].Value = model.toname;
            parameters[6].Value = model.ccname;
            parameters[7].Value = model.sendtime;
            parameters[8].Value = model.fromname;
            parameters[9].Value = model.guid;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);

            if (obj == null)
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
コード例 #5
0
ファイル: maillist.cs プロジェクト: redmantis/SendEmail
        public Mail_Test.Model.maillist GetModel(Guid guid)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 id,title,mailcontent,mto,mcc,mfrom,toname,ccname,sendtime,fromname,guid from maillist ");
            strSql.Append(" where guid=@guid");
            SqlParameter[] parameters =
            {
                new SqlParameter("@guid", SqlDbType.UniqueIdentifier, 16)
            };
            parameters[0].Value = guid;

            Mail_Test.Model.maillist model = new Mail_Test.Model.maillist();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return(DataRowToModel(ds.Tables[0].Rows[0]));
            }
            else
            {
                return(null);
            }
        }
コード例 #6
0
        public JsonResult Sendmail()
        {
            bool   isAsync = false;
            string idlist  = "";

            if (Request.Form["selmember"] != null)
            {
                idlist = Request.Form["selmember"].ToString();
            }
            idlist = idlist.Replace("false", "0");


            MailHelper mail = new MailHelper(isAsync);


            //保存邮件信息
            Mail_Test.Model.maillist mailinfo = new Mail_Test.Model.maillist();
            mailinfo.mcc         = Request.Form["txtCc"].ToString();
            mailinfo.ccname      = Request.Form["txtCcname"].ToString();
            mailinfo.fromname    = Request.Form["txtfromname"].ToString();
            mailinfo.mailcontent = Request.Form["txtContent"].ToString();
            mailinfo.mfrom       = Request.Form["txtfrom"].ToString();
            mailinfo.mto         = Request.Form["txtTo"].ToString();
            mailinfo.title       = Request.Form["txtTitle"].ToString();
            mailinfo.toname      = Request.Form["txtToname"].ToString();
            mailinfo.sendtime    = DateTime.Now;
            mailinfo.guid        = new Guid(Request.Form["guid"].ToString());
            int mailid = new Mail_Test.BLL.maillist().Add(mailinfo);

            string    path = AppDomain.CurrentDomain.BaseDirectory + "uploads\\" + mailid.ToString() + "\\";
            ArrayList list = Config.uploadfile(System.Web.HttpContext.Current.Request.Files, path);

            savefj(list, mailid);

            if (Config.IsEmailString(mailinfo.mto))
            {
                this.SendMessage(mail, false, list, mailinfo, true, true);
            }

            //保存收件人信息
            DataSet ds = getmemberlist(idlist);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Mail_Test.Model.sendlist model = new Mail_Test.Model.sendlist();
                model.mailid   = mailid;
                model.userid   = int.Parse(dr["id"].ToString());
                model.issend   = false;
                model.sendtime = DateTime.Now;
                new sendlist().Add(model);
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                mailinfo.mto    = dr["useremail"].ToString();
                mailinfo.toname = dr["username"].ToString();
                this.SendMessage(mail, false, list, mailinfo, true, true);

                Mail_Test.Model.sendlist model = new Mail_Test.Model.sendlist();
                model.mailid   = mailid;
                model.userid   = int.Parse(dr["id"].ToString());
                model.issend   = true;
                model.sendtime = DateTime.Now;
                new sendlist().Update(model);
            }
            JsonResult jsr = new JsonResult();

            jsr.Data = "邮件发送完成";
            return(jsr);
        }
コード例 #7
0
        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="isSimple">是否只发送一条</param>
        /// <param name="autoReleaseSmtp">是否自动释放SmtpClient</param>
        /// <param name="isReuse">是否重用SmtpClient</param>
        private void SendMessage(MailHelper mail, bool isSimple, ArrayList list, Mail_Test.Model.maillist mailinfo, bool autoReleaseSmtp, bool isReuse)
        {
            mail.IsBodyHtml = true;

            string bcc = "";//密送

            if (mailinfo.mto.Length > 0)
            {
                mail.AddReceive(EmailAddrType.To, mailinfo.mto, mailinfo.toname);
            }
            if (mailinfo.mcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.CC, mailinfo.mcc, mailinfo.ccname);
            }

            if (bcc.Length > 0)
            {
                mail.AddReceive(EmailAddrType.Bcc, bcc, Config.GetAddressName(bcc));
            }

            mail.Subject = mailinfo.title;

            // Guid.NewGuid() 防止重复内容,被SMTP服务器拒绝接收邮件
            mail.Body            = mailinfo.mailcontent;
            mail.From            = mailinfo.mfrom;
            mail.FromDisplayName = mailinfo.fromname;

            if (!isReuse || !mail.ExistsSmtpClient())
            {
                mail.SetSmtpClient(
                    new SmtpHelper(Config.TestEmailType, false, Config.TestUserName, Config.TestPassword).SmtpClient
                    , autoReleaseSmtp
                    );
            }

            if (list != null && list.Count > 0)
            {
                foreach (string filePath in list)
                {
                    mail.AddAttachment(filePath);
                }
            }


            Dictionary <MailInfoType, string> dic = mail.CheckSendMail();

            if (dic.Count > 0 && MailInfoHelper.ExistsError(dic))
            {
                // 反馈“错误+提示”信息
                ViewBag.msg += MailInfoHelper.GetMailInfoStr(dic);
            }
            else
            {
                string msg = String.Empty;
                if (dic.Count > 0)
                {
                    // 反馈“提示”信息
                    ViewBag.msg = MailInfoHelper.GetMailInfoStr(dic);
                }

                try
                {
                    if (isSimple)
                    {
                        mail.SendOneMail();
                    }
                    else
                    {
                        // 发送
                        mail.SendBatchMail();
                    }
                }
                catch (Exception ex)
                {
                    // 反馈异常信息
                    ViewBag.msg += (ex.InnerException == null ? ex.Message : ex.Message + ex.InnerException.Message) + Environment.NewLine;
                }
                finally
                {
                    // 输出到界面
                    if (msg.Length > 0)
                    {
                        ViewBag.msg += Environment.NewLine;
                    }
                }
            }

            mail.Reset();
        }