Writing of the mass mailing tool (3) Several Methods of sending mails

Source: Internet
Author: User
Tags website server mailmessage smtpclient

In the previous article, the mass mailing tool (ii) saved data. We talked about saving mailbox data.

 

In this article, let's talk about how to send a group of emails to the center.

First, let's introduceProgramThere are several ways to send emails.

1. SMTP mode.

This method is silly, and it is relatively simple to call in C # (other languages such as Java have similar pocket (packages) that can be called directly, rather simple ). In. net, classes with two threads can be used:

One is system. net. mail, and the other is sytem. Web. Mail. Of course there are no other classes I have not gone. However, the later class has already been prompted in Vs, so use the previous class.

The call method is as follows. First Add a reference to system. net. Mail.CodeAs follows:

View code

 1   ///   <Summary>  2      ///  Author: early morning searcher 3      ///  URL:  Http://www.cnblogs.com/uu102  4      ///   </Summary>  5       Public   Class  Mailsender  6   {  7          Public Smtpclient client { Get ;Set  ;}  8          Public Mailmessage MSG { Get ; Set  ;}  9          Private   String [] { Get ; Set  ;}  10          Private   String Smtpserver {Get ; Set  ;}  11          Private   Int Port { Get ; Set  ;}  12          Public  Mailsender ()  13   {  14              15   } 16   17          Public Mailsender ( String   From , String Smtpserver, String Username, String  Password)  18   {  19              This . INIT ( From  , Smtpserver, username, password ); 20   }  21          Private   Void Init ( String   From , String Smtpserver, String Username, String  Password)  22   {  23              This . MSG =New  Mailmessage ();  24              This . Client = New  Smtpclient (smtpserver );  25              This . Smtpserver = smtpserver; //  SMTP server address of the email sending Address  26              This . Client. usedefacrecredentials = True  ;  27             This . Client. Credentials = New Networkcredential (username, password ); //  Here is the username and password required for logging on to the mailbox.  28              This . Msg. From = New Mailaddress ( From ); //  Set email sending Address  29              This . Msg. isbodyhtml = True ; // It is best to set the email body in HTML format.  30               This . Msg. bodyencoding = encoding. utf8; //  Body code  31              This . Msg. subjectencoding = Encoding. utf8;  32              This . Msg. deliverynotificationoptions = deliverynotificationoptions. onfailure; //  Notify me when the email fails to be sent.  33              34  }  35          Public   Void Send ( String Subject, String Body, String  [] Attachmentpaths)  36   {  37   38              39              For ( Int I = 0 ; I <attachmentpaths. length; I ++ )  40   {  41                  This . Msg. attachments. Add ( New  Attachment (attachmentpaths [I]);  42   }  43              This . Msg. Subject = Subject;  44              This . Msg. Body =Body;  45               This . Client. Send ( This  . Msg );  46               47   }  48 }

 

This email sending encapsulation class does not play a role in encapsulation. I am waiting for you to complete the Code. The Code is very simple. If you call it, please refer to the following code. View code

 1    ///   <Summary>  2          ///  Copyright: No  3           ///  Author: early morning searcher  4           ///  Site:  Http://www.cnblogs.com/uu102  5           ///   </Summary>  6           ///   <Param name = "sender"> </param>  7          ///   <Param name = "E"> </param>  8           Private   Void Button3_click ( Object  Sender, eventargs E)  9   {  10              Try  11   {  12 Mailsender = New Mailsender ("  Wxp146@qq.com  " , "  Smtp.qq.com  " , "  Wxp146  " , "  ******  " ); //  Rember that it's your password  13 Links =New Dictionary < String , Linkman> ();  14                   For ( Int I = 0 ; I <listview1.checkeditems. Count; I ++ )  15   {  16 Listviewitem = Listview1.items [listview1.checkeditems [I]. Index];  17 Linkman = New  Linkman ();  18                       String Id = Listviewitem. text;  19                       String Nick = listviewitem. subitems [ 1  ]. Text;  20                       String Email = listviewitem. subitems [ 2  ]. Text;  21                      String Area = listviewitem. subitems [ 3  ]. Text;  22 Linkman. Nick = Nick;  23 Linkman. Email = Email;  24 Linkman. Area = Area;  25   Mailsender. msg. to. Add (linkman. Email );  26   27  28   Links. Add (linkman. Email, linkman );  29   30   }  31                  32                   /*  33   * Mailsender. msg. Subject = "this is a test letter. You can directly put it in the garbage bin! ";  34   Mailsender. msg. Body = "Hello: Try it. Sorry for the exception! <A href = \ "http: // www.uu102.com \"> sugon Marketing Technology Forum </a> ";  35  Mailsender. Client. Send (mailsender. msg );  36   *  37                    */  38 Mailsender. Send ( "  This is a test letter, which can be directly stored in the garbage bin!  "  ,  39                                    "  Hello, please try again. Sorry for the exception! <A href = \ "http: // www.uu102.com \"> sugon Marketing Technology Forum </a>  "  , 40                                    New   String [] {Environment. currentdirectory + "  \ Config \ mailinfo. xml  "  });  41 MessageBox. Show ( "  Email sent successfully  "  );  42   }  43               Catch (Webexception E1)  44   {  45 MessageBox. Show ( String . Format ( "  Failed to send email. Please check settings {0}  "  , E1.message ));  46   }  47 }

 

As for the code above, button3 suddenly appears as a button for clicking send. The above is the first mail sending method we will introduce.

2. log on to the mailbox Management page, and then simulate sending.

A typical practice is to log on to the QQ mailbox and post a post. Many people are studying this method on the Internet. Of course, due to the relationship between time and energy, I did not directly paste the Code. If you are interested, contact me and discuss it together.

3. Use the SMTP mailbox service of IIS to send

This method is probably the most troublesome. It may not succeed if you want to install it on your own machine. Of course, if you have a personal website server, this is also a solution.

 

Of course there are many other or improved methods, such as SSL secure encrypted transmission and socket transmission (this method will not be available in the future ).

The above are the basic factors for sending mails. If you want to send mails, of course, this is not enough. There are still many factors to consider. I hope you can complete it yourself.

Next, let's talk about the mail tracking service after mass mailing. This is a matter that many people are currently concerned about. This is what we will talk about in the mail sending process. The tutorial is updated every day. Please stay tuned!

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.