引言
经常会参与组织一些社区活动,涉及到和不同的人进行交流,微信当然是必须的,同样邮件也是一种不可或缺的方式。
一般群发的邮件不是很友好,如果是一对一的,收到邮件的人是不是会比较重视,而且还有他的名字在里面。
所以抽点时间写了一个工具,使用C#做个发邮件的工具非常简单。
完整代码
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 { get; set; } public string Email { get; set; } } }
app.config
<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 **
代码下载
时间: 2024-10-01 12:50:00