Send mail in C #

Source: Internet
Author: User
Tags mailmessage smtpclient

There are 2 ways to do this:

The first type:

usingSystem;usingSystem.Net.Mail;//at that time the test is QQ mailboxclasstestmail{Static voidMain (string[] args) {        stringMailcontent ="Message Content"; MailMessage msg=NewSystem.Net.Mail.MailMessage (); Msg. To.add ("[email protected],[email protected]");//Recipient//Sender InformationMsg. from =NewMailAddress ("[email protected]","Sender's name", System.Text.Encoding.UTF8); Msg. Subject="This is a test message .";//message HeaderMsg. subjectencoding = System.Text.Encoding.UTF8;//Title EncodingMsg. Body = mailcontent;//message bodyMsg. Bodyencoding =System.Text.Encoding.UTF8; Msg. Isbodyhtml=true;//whether the HTMLMsg. priority = Mailpriority.high;//Priority LevelSmtpClient client =NewSmtpClient (); //Note: The following password is the authorization code, not your password to log in QQ, view method: QQ Mailbox---settings insideClient. Credentials =NewSystem.Net.NetworkCredential ("[email protected]","ASDFGHJKL"); Client. Enablessl=true; Client. Port=587;//443Client. Host ="smtp.qq.com";//mail.adient.com        ObjectUserState =msg; Try{client.            Send (msg); //MessageBox.Show ("sent successfully");        }       Catch(Exception ex) {//MessageBox.Show (ex. Message, "Error sending message");       }    }}

The second type:

//The mailbox of Outlook Yanfeng was tested at the timeusingSystem.Net.Mail;namespaceconsoleapplication11{classProgram {Static voidMain (string[] args) {MailMessage mail=NewMailMessage (); //set the title of a messageMail. Subject ="Test Message"; //set the sender of a message//Mail.            from = new MailAddress ("[Email protected]", "xiao_1006"); //Sender's e-mail address, followed by parameter Zhang Meng: The recipient sees the name of the senderMail. from =NewMailAddress ("[email protected]","Zhang Meng"); //set the recipient of the message, Zmeng no useMail. To.add ("4[email protected],[email protected]"); //set up cc people for a message//Mail. Cc.            ADD (New MailAddress ("[Email protected]", "Nick")); //set the contents of a messageMail. Body ="is the test with 111 ."; Mail. Bodyencoding=System.Text.Encoding.UTF8; Mail. Isbodyhtml=true; Mail. Priority=Mailpriority.normal; Mail. Deliverynotificationoptions=deliverynotificationoptions.onsuccess; SmtpClient Client=NewSmtpClient (); //the name of the mail server, to ask the other company only knowClient. Host ="smtp.ga.adient.com"; //E -Mail The port number, to ask the other company only knowClient. Port = -; Client. useDefaultCredentials=false; //client.            Credentials = new System.Net.NetworkCredential ("[Email protected]", "xiao_1006"); //Sender's account number and passwordClient. Credentials =NewSystem.Net.NetworkCredential ("[email protected]","Yfas123"); Client. Deliverymethod=smtpdeliverymethod.network; Client.        Send (mail); }    }}

Send mail in C #

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.