zoukankan      html  css  js  c++  java
  • 邮件批量发送

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net.Mail;
    using System.Configuration;
    using System.Collections;
    using System.Data.SqlClient;
    public partial class _Default : System.Web.UI.Page
    {
        public string mailContent ;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Send()
        {
            MailMessage mail = new MailMessage();
            try
            {
                //设置邮件的标题
                mail.Subject = "悠哉WebService报告";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                //设置邮件的发件人
                string SendEmail = ConfigurationManager.AppSettings["SendEmail"].ToString();
                MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //邮件的发件人
                mail.From = Addressfrom;
                /*  这里这样写是因为可能发给多个联系人,每个地址用 , 号隔开
               */
                List<string> mailAddress = new List<string>();
                foreach (string address in mailAddress)
                {
                    if (address != string.Empty)
                    {
                        mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
                    }
                }
                //设置邮件的内容
                mail.Body = mailContent;
                //设置邮件的格式
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                //mail.IsBodyHtml = true;
                //设置邮件的发送级别
                mail.Priority = MailPriority.Normal;
                //发送通知
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                SmtpClient client = new SmtpClient();
                //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
                client.Host = "smtp.gmail.com";
                client.Port = 25;
                client.Timeout = 9999;
                client.UseDefaultCredentials = true;
                client.EnableSsl = true;
                //邮箱登陆名和密码
                string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
                string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
                client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //发送email
                client.Send(mail);
            }
            catch (Exception)
            {
                //当邮件发送失败,发送异常时 使用备用方法调用备用邮箱发送
                SendBackUp();
            }
            finally
            {
                mail.Dispose();
            }
        }
        public void SendBackUp()
        {
            MailMessage mail = new MailMessage();
            try
            {
                //设置邮件的标题
                mail.Subject = "悠哉WebService报告";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                //设置邮件的发件人
                string SendEmail = System.Configuration.ConfigurationManager.AppSettings["SendEmail"].ToString();
                MailAddress Addressfrom = new MailAddress(SendEmail, "uzai", System.Text.Encoding.UTF8); //邮件的发件人
                mail.From = Addressfrom;
                /*  这里这样写是因为可能发给多个联系人,每个地址用 , 号隔开
               */
                List<string> mailAddress = new List<string>();
                foreach (string address in mailAddress)
                {
                    if (address != string.Empty)
                    {
                        mail.To.Add(new MailAddress(address, address, System.Text.Encoding.UTF8));
                    }
                }
                //设置邮件的内容
                mail.Body = mailContent;
                //设置邮件的格式
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                //mail.IsBodyHtml = true;
                //设置邮件的发送级别
                mail.Priority = MailPriority.Normal;
                //发送通知
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                SmtpClient client = new SmtpClient();
                //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
                client.Host = "smtp.gmail.com";
                client.Port = 25;
                client.Timeout = 9999;
                client.UseDefaultCredentials = true;
                client.EnableSsl = true;
                //邮箱登陆名和密码
                string EmailLogName = ConfigurationManager.AppSettings["EmailLogName"].ToString();
                string EmailLogPwd = ConfigurationManager.AppSettings["EmailLogPwd"].ToString();
                client.Credentials = new System.Net.NetworkCredential(EmailLogName, EmailLogPwd);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                //发送email
                client.Send(mail);
            }
            catch (Exception)
            {
                //当邮件发送失败,发送异常时 使用备用方法调用备用邮箱发送
                Send();
            }
            finally
            {
                mail.Dispose();
            }
        }
    }

  • 相关阅读:
    UIScrollView设置滑动的距离
    iOS动画效果和实现
    cmd 编码 utf8
    详细的ifcfg-eth0配置详解
    Centos下 Nginx安装与配置
    linux安装目录
    utf8 和 UTF-8 的区别
    后台添加搜索功能讲解
    移动端 禁横向滚动
    PC端 $_SERVER 说明
  • 原文地址:https://www.cnblogs.com/weihengblogs/p/3908271.html
Copyright © 2011-2022 走看看