• 邮件群发


    邮件群发工具(C#版)

    引言

     

    经常会参与组织一些社区活动,涉及到和不同的人进行交流,微信当然是必须的,同样邮件也是一种不可或缺的方式。

     

    一般群发的邮件不是很友好,如果是一对一的,收到邮件的人是不是会比较重视,而且还有他的名字在里面。

     

    所以抽点时间写了一个工具,使用C#做个发邮件的工具非常简单。

     

     

    完整代码

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    class Program
        {
            private static string baseDir = "";
            private static string email = "";
     
            static void Main(string[] args)
            {
                Console.WriteLine("--- 开始执行 --- ");
                baseDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                email = ConfigurationManager.AppSettings["email"];
     
                var date = DateTime.Now.ToString("yyyy-MM-dd");
                FileStream fs = new FileStream($"{baseDir}\[Log]{date}.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("===== 发送日志 =====");
     
                List<Contact> contacts = GetContacts();
                var smtpClient = GetSmtpClient();
                foreach (var contact in contacts)
                {
                    SendMail(smtpClient, contact, sw);
                }
                 
                sw.WriteLine("===== 执行完成 =====");
                sw.Flush();
                sw.Close();
                fs.Close();
                Console.WriteLine("--- 执行完成 --- ");
                Console.ReadLine();
            }
     
            private static SmtpClient GetSmtpClient()
            {
                string server = ConfigurationManager.AppSettings["server"];
                string port = ConfigurationManager.AppSettings["port"];
                string password = ConfigurationManager.AppSettings["password"];
     
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = server;
                smtpClient.Port = Convert.ToInt32(port);
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new NetworkCredential(email, password);
                return smtpClient;
            }
     
            private static void SendMail(SmtpClient smtpClient, Contact contact, StreamWriter sw)
            {
                try
                {
                    var mailMessage = GetMailMessage(contact);
                    smtpClient.Send(mailMessage);
                    Console.WriteLine($" --- 发送成功, Email = {contact.Email} --- ");
                    sw.WriteLine($" --- 发送成功, Email = {contact.Email} --- ");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($" === 发送失败, Email = {contact.Email} === ");
                    sw.WriteLine($" === 发送失败, Email = {contact.Email} === ");
                }
            }
     
            private static MailMessage GetMailMessage(Contact contact)
            {
                string subject = ConfigurationManager.AppSettings["subject"];
                string introduce = ConfigurationManager.AppSettings["introduce"];
                string link = ConfigurationManager.AppSettings["link"];
                string content = "<p style="font - size:16px">Dear " + contact.Name + " , </p> " +
                                     "<p style="font - size:16px">" + introduce + "</p>"+
                                     "<p style="font - size:16px">报名链接:<a target="_blank" href="" + link + "">"+ link + "</a></p>"
                                     + GetContent();
                MailMessage mailMessage = new MailMessage(email, contact.Email);
                mailMessage.Subject = subject;
                mailMessage.Body = content;
                mailMessage.BodyEncoding = Encoding.UTF8;
                mailMessage.IsBodyHtml = true;
                mailMessage.Priority = MailPriority.Normal;
                return mailMessage;
            }
     
            private static string GetContent()
            {
                var dir = baseDir + "\content.txt";
                StreamReader sr = new StreamReader(dir, Encoding.UTF8);
                string content = sr.ReadToEnd();
                sr.Close();
                return content;
            }
     
            private static List<Contact> GetContacts()
            {
                List<Contact> contacts = new List<Contact>();
                var dir = baseDir + "\contacts.txt";
                StreamReader sr = new StreamReader(dir, Encoding.UTF8);
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    line = line.Replace(","",");
                    var contact = line.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
                    if (contact.Length == 2 && !string.IsNullOrEmpty(contact[0]) && !string.IsNullOrEmpty(contact[1]))
                    {
                        contacts.Add(new Contact() {Name = contact[0], Email = contact[1]});
                    }
                }
                sr.Close();
                return contacts;
            }
     
            public class Contact
            {
                public string Name { getset; }
     
                public string Email { getset; }
            }
        }

     

     

    app.config

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <appSettings>
      <add key="server" value="smtp.live.com" />
      <add key="port" value="25" />
      <add key="email" value="***@hotmail.com" />
      <add key="password" value="" />
      <add key="subject" value="敏捷个人北京2016年6月活动:玩转生涯狂想曲" />
      <add key="introduce" value="欢迎参加敏捷个人北京2016年6月活动:玩转生涯狂想曲活动!" />
      <add key="link" value="http://www.hdb.com/party/qz1eu?h_share_uid=f0bv" />
    </appSettings>

     

     

    注意

     

    1)读取app.config的配置信息

     

    2)读取程序根目录下的联系人文件:contacts.txt

     

    --------------------------------

     

    测试1,***@gmail.com
    测试2,***@qq.com
    测试3,***@163.com

     

    --------------------------------

     

    3)读取程序根目录下的内容文件,包含Html代码:content.txt

     

     

    发送结果截图

     

    1)控制台输出日志

     

     

    2)收到邮件内容,dear **

     

     

     

     

    代码下载

     

     下载地址

  • 相关阅读:
    P3507 [POI2010]GRA-The Minima Game
    P2038 无线网络发射器选址
    2017.9.23清北第二场
    P3183 [HAOI2016]食物链
    2017.9.17校内noip模拟赛解题报告
    Day2代码
    P1328 生活大爆炸版石头剪刀布
    Fibinary Numbers
    Mac os 进行Android开发笔记(1)
    python中文注释及输出出错
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5616083.html
Copyright © 2020-2023  润新知