protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            string      receiver    = this.txtReceiver.Text.Trim();
            string      subject     = this.txtSubject.Text.Trim();
            MailMessage mailMessage = new MailMessage();

            string content = this.hideMailContent.Value;

            mailMessage.From       = "*****@*****.**";
            mailMessage.Subject    = subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.HtmlBody   = content;
            string[] users = receiver.Split(',');
            foreach (string user in users)
            {
                int    startIndex = user.IndexOf('<') + 1;
                int    length     = user.Length;
                string email      = user.Substring(startIndex, length - startIndex - 1);
                mailMessage.To.Add(email);
            }
            //创建附件
            if (Session["tempTable"] != null)
            {
                DataTable table = (DataTable)Session["tempTable"];
                foreach (DataRow row in table.Rows)
                {
                    mailMessage.Attachments.Add(new Attachment(row["FilePath"].ToString()));
                }
            }
            SmtpClient client = AsposeUtilities.GetSmtpClient(this.Request.ApplicationPath);

            try
            {
                client.Send(mailMessage);
                using (var context = new EmailContext())
                {
                    var entity = new SendLog
                    {
                        Sender   = CookieUtilities.GetCookieValue("username"),
                        Receiver = receiver,
                        SendTime = DateTime.Now,
                        SendType = "Email"
                    };
                    context.SendLogs.Add(entity);
                    context.SaveChanges();
                }
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, GetType(), "a", "alert('邮件发送成功')", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, GetType(), "a", "alert('邮件发送失败!" + ex.Message + "')", true);
            }
        }
Example #2
0
        protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            string      emailPath = Request.MapPath("~/ReportManage/template_files/mail.html");
            MailMessage msg       = new MailMessage();
            string      emails    = this.txtEmail.Text.Replace(',', ' ').Replace(';', ' ');

            for (int i = 0; i < emails.Split(' ').Length; i++)
            {
                msg.To.Add(emails.Split(' ')[i]);
            }
            msg.From       = "*****@*****.**";
            msg.Subject    = this.txtSubject.Text;
            msg.IsBodyHtml = true;
            msg.HtmlBody   = this.txtEmailContent.Text;
            if (!String.IsNullOrEmpty(this.txtFilePath1.Text))
            {
                Attachment attachment = new Attachment(Server.MapPath(this.txtFilePath1.Text));
                msg.Attachments.Add(attachment);
            }
            if (!String.IsNullOrEmpty(this.txtFilePath2.Text))
            {
                Attachment attachment = new Attachment(Server.MapPath(this.txtFilePath2.Text));
                msg.Attachments.Add(attachment);
            }
            if (!String.IsNullOrEmpty(this.txtFilePath3.Text))
            {
                Attachment attachment = new Attachment(Server.MapPath(this.txtFilePath3.Text));
                msg.Attachments.Add(attachment);
            }
            SmtpClient client = AsposeUtilities.GetSmtpClient(this.Request.ApplicationPath);

            try
            {
                client.Send(msg);
                MessageBox.ShowAndRedirect(this, "邮件发送成功", this.Request.Path);
            }
            catch (Exception ex)
            {
                MessageBox.ShowAndRedirect(this, "邮件发送失败", this.Request.Path);
            }
        }