Bulk Mail Tool (C # Edition)

Source: Internet
Author: User
Tags mailmessage smtpclient

Introduction

It is often necessary to be involved in organizing community activities that involve communicating with different people, and the same email is an indispensable way.

The general bulk of the mail is not very friendly, if it is one-on, the person receiving the mail will be more attention, but also his name in the inside.

So take some time to write a tool, using C # to make a mail-sending tool is very simple.

Full code
Class Program {private static string baseDir = "";        private static string email = "";            static void Main (string[] args) {Console.WriteLine ("---begins execution---");            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 ("===== send log =====");            list<contact> contacts = Getcontacts ();            var smtpclient = getsmtpclient ();            foreach (var contact in Contacts) {SendMail (smtpclient, contact, SW); } SW.            WriteLine ("===== execution completed ====="); Sw.            Flush (); Sw.            Close (); Fs.            Close ();            Console.WriteLine ("---execution completed---"); Console.ReadLine (); } private static SmtpClient Getsmtpclient () {String server = configurationmanager.appsettings["s            Erver "];            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 ($ "---sent successfully, email = {Contact.                Email}---"); Sw. WriteLine ($ "---send success, email = {Contact.            Email}---"); } catch (Exception ex) {Console.WriteLine ($ = = = = Send Failed, email = {Contact.                Email} = = = "); Sw. WriteLine ($ "= = = Send Failed, email = {Contact.            Email} = = = "); }} private static MailMessage getmailmessage (Contact contact) {String subject = Configur            ationmanager.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\" > Registration Link: "
                    + "<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<c Ontact> (); 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.
  <appSettings>    <add key= "Server" value= "smtp.live.com"/>    <add key= "port" value= "/>"    <add key= "Email" value= "* * * @hotmail. com"/>    <add key= "password" value= ""/> <add    key= " Subject "value=" agile personal Beijing June 2016 activities: Play turn career Rhapsody "/>    <add key=" introduce "value=" Welcome to participate in the agile individual Beijing June 2016 activities: Play a career rhapsody activities! >    <add key= "link" value= "HTTP://WWW.HDB.COM/PARTY/QZ1EU?H_SHARE_UID=F0BV"/>  </appsettings >

Attention

1) Read configuration information for App. Config

2) Read the contact file under the program root: Contacts.txt

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

Test 1,*** @gmail. com
Test 2,*** @qq. com
Test 3,***@163.com

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

3) Read the content file in the root directory of the program, including HTML code: Content.txt

Send results

1) Console output log

2) Receive email content,Dear * *

Code download

Bulk Mail Tool (C # Edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.