发送邮件(单发群发的解决方案)

来源:互联网 发布:公路工程造价软件 编辑:程序博客网 时间:2024/05/22 05:13

#region 发送邮件处理
        private void boolSend()
        {
            try
            {

                string from = dlEmail.SelectedItem.Text.ToString();//发送邮件
                string fromName = "";//发件人  
                string toEmail = this.txtRecipient.Text.ToString().Trim();//收件邮箱
                string cc = this.txtcc.Text.ToString().Trim();//抄送
                string bcc = this.txtbcc.Text.ToString().Trim();//密送
                string subject = txtsubject.Text.ToString().Trim();//发送主题
                string body = txtbody.Text.ToString().Trim();//发送内容
                string filepath = FileUpload1.PostedFile.FileName;//附件地址
                HFWDataSet dataSet = email.getById(dlEmail.SelectedValue.ToString());
                string ecode = dataSet.getString("ecode");//登陆名
                string pwd = dataSet.getString("pwd");//登陆密码
                string eHostId = dataSet.getString("eConfigid");
                dataSet = email.getAllEmailById(eHostId);
                string host = dataSet.getString("EmailHost");//邮件服务器地址
                int port = dataSet.getInt("EmailPort");//端口号
                bool enableSsl = dataSet.getBool("enableSsl");//是否加密连接
                string[] str = toEmail.Split(';');
                if (str.Length == 1)//当个邮件发送
                {
                    int i = email.emailSend(from, fromName, str[0], cc, bcc, subject, body, filepath, ecode, pwd, host, port, enableSsl);
                    if (i == 1)
                    {
                        dataSet = email.getEmailInfoById("-1");
                        dataSet.insert();
                        dataSet.setString("userid", CurrentLoginUser.Id.ToString());
                        dataSet.setString("fromEmail", from);
                        dataSet.setString("toEmail", toEmail);
                        dataSet.setString("fromName", fromName);
                        dataSet.setString("eSubject", subject);
                        dataSet.setString("eBody", body);
                        dataSet.setInt("state", 1);
                        dataSet.setDateTime("creatTime", DateTime.Now);
                        email.insert(dataSet);
                    }
                }
                else
                {
                    for (int n = 0; n < str.Length - 1; n++)//群发 分别发送,要用分号隔开
                    {
                        int i = email.emailSend(from, fromName, str[n], cc, bcc, subject, body, filepath, ecode, pwd, host, port, enableSsl);

                        if (i == 1)
                        {
                            dataSet = email.getEmailInfoById("-1");
                            dataSet.insert();
                            dataSet.setString("userid", CurrentLoginUser.Id.ToString());
                            dataSet.setString("fromEmail", from);
                            dataSet.setString("toEmail", toEmail);
                            dataSet.setString("fromName", fromName);
                            dataSet.setString("eSubject", subject);
                            dataSet.setString("eBody", body);
                            dataSet.setInt("state", 1);
                            dataSet.setDateTime("creatTime", DateTime.Now);
                            email.insert(dataSet);
                        }

                        else
                        {
                            MessageBox("发送失败");
                        }
                    }
                   
                }
                MessageBox("发送成功", "sentbox.aspx");
            }
            catch
            {
                MessageBox("发送失败");
            }
        #endregion

 

 

 

        /// <summary>
        /// Email发送
        /// </summary>
        public int emailSend(string from, string name, string to,string cc,string bcc, string subject, string body,string filepath, string ecode, string pwd, string host, int port, bool enableSsl)
        {
            MailMessage myMail = new MailMessage();

            myMail.From = new MailAddress(from, name, Encoding.UTF8);
            myMail.To.Add(new MailAddress(to));
            if (cc != "")
            {
                myMail.CC.Add(new MailAddress(cc));
            }
            if (bcc != "")
            {
                myMail.Bcc.Add(new MailAddress(bcc));
            }
            myMail.Subject = subject;

            myMail.SubjectEncoding = Encoding.UTF8;

            myMail.Body = body;

            myMail.BodyEncoding = Encoding.UTF8;

            myMail.IsBodyHtml = true;

            myMail.Priority = MailPriority.High;
            if (filepath != "")
            {
                myMail.Attachments.Add(GetAttachment(filepath));
            }
            int i = email.emailSend(myMail, new NetworkCredential(ecode, pwd), host, port, enableSsl);
            return i;
        }
        /// <summary>
        /// 判断附件容量
        /// </summary>
        /// <param name="file">文件地址</param>
        /// /// <param name="MaxSize">最大容量,单位:M</param>
        /// <returns></returns>
        public static Attachment GetAttachment(string file, int MaxSize)
        {
            if (!File.Exists(file))
            {

                return null;
            }
            int size = 0;
            FileStream fs = new FileStream(file, FileMode.Open);
            string name = fs.Name;
            size = (int)(fs.Length / 1024);
            fs.Close();

            //控制文件大小不大于10M
            if (size > 1024 * MaxSize)
            {
                HISearch.BIZ.HFW.view.HFWBasePage messbox = new HISearch.BIZ.HFW.view.HFWBasePage();
                messbox.MessageBox("附件容量过大!");
                return null;
            }
            Attachment att = new Attachment(file, MediaTypeNames.Application.Octet);
            ContentDisposition disposition = att.ContentDisposition;
            disposition.CreationDate = System.IO.File.GetCreationTime(file);
            disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
            disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
            return att;
        }

        /// <summary>
        /// 判断附件容量
        /// </summary>
        /// <param name="file">文件地址</param>
        /// <returns></returns>
        public static Attachment GetAttachment(string file)
        {
            return GetAttachment(file, 10);
        }

 

 

 #region/// 描述: 邮件发送
        ///
        /// 修改记录:
        /// 1. 09/09/15 PM chenqp 创建
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        #endregion
        public int emailSend(MailMessage mailMsg, NetworkCredential credential,string host,int port,bool enableSsl)
        {
            try
            {
                //smtp client

                SmtpClient senderC = new SmtpClient();

                senderC.Host = host;
                senderC.Port =port;

                senderC.UseDefaultCredentials = false;

                senderC.Credentials = credential;

                senderC.DeliveryMethod = SmtpDeliveryMethod.Network;

                senderC.EnableSsl = enableSsl;

               
                return (mailSend(mailMsg, senderC));
            }
            catch
            {
                return 0;
            }
            return 1;
        }

 

        /// <summary>
        /// Email 发送
        /// </summary>
        /// <param name="mailMsg"></param>
        /// <param name="smtpClient"></param>
        private int mailSend(MailMessage mailMsg, SmtpClient smtpClient)
        {
            if (smtpClient != null)
            {
                try
                {
                    smtpClient.Send(mailMsg); //smtp client
                    return 1;
               
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
            return 1;

        }

原创粉丝点击