C #, simple mail sending function, mail auxiliary class,

Source: Internet
Author: User
Tags mailmessage smtpclient

C #, simple mail sending function, mail auxiliary class,

This help class can only send simple basic emails to multiple users with a single content. The copy function is not available. Add attachments. The function code is as follows,

Private void button3_Click (object sender, EventArgs e) {int result = SendEmail ("123456@qq.com", "123456", new string [] {"530794089@qq.com"}, "test title ", "Test content", "smtp.qq.com"); MessageBox. show (result. toString ());} /// <summary> /// send an email /// </summary> /// <param name = "sendEmailAddress"> sender's email address </param> /// <param name = "sendEmailPwd"> sender's password </param> /// <param name = "msgToEmail"> recipient's email address </param> /// <param name = "Title"> email title </param> /// <param name = "content"> email content </param> /// <param name = "host"> SMTP server </param> /// <returns> 0: failed. 1: Successful! </Returns> public static int SendEmail (string sendEmailAddress, string sendEmailPwd, string [] msgToEmail, string title, string content, string host) {// sender's email address string fjrtxt = sendEmailAddress; // sender's email password string mmtxt = sendEmailPwd; // subject string zttxt = title; // content string nrtxt = content; string [] fasong = fjrtxt. split ('@'); // set the mail protocol SmtpClient client = new SmtpClient (host); // System. net. mail. smtpC Lient client. useDefaultCredentials = false; // sent to the Smtp server client over the network. deliveryMethod = SmtpDeliveryMethod. network; // use the user name and password to authenticate the client. credentials = new NetworkCredential (fasong [0]. toString (), mmtxt); // System. net. networkCredential // set SmtpClient for ssl encryption in QQ mail. if the EnableSsl attribute is True, "specify SmtpClient to use Secure Socket Layer (SSL) to encrypt the connection." Client. enableSsl = true; // the sender and recipient's email address MailMessage mmsg = new MailMessage (); mmsg. from = new MailAddress (fjrtxt); for (int I = 0; I <msgToEmail. length; I ++) {mmsg. to. add (new MailAddress (msgToEmail [I]);} // Email Subject mmsg. subject = zttxt; // The Subject code is mmsg. subjectEncoding = Encoding. UTF8; // Message Body (mmsg. body = nrtxt; // The Body code is mmsg. bodyEncoding = Encoding. UTF8; // set it to the HTML format of mmsg. isBodyHtml = true; // priority mmsg. priority = MailPriority. high; try {client. send (mmsg); return 1;} catch (Exception exss) {string msg = exss. message; return 0 ;}}

It is worth noting that when using QQ mail, the sender's password is an independent QQ Mail Password.

The test is effective in the winform program. The use on the web page is to be tested.

Welcome to discuss C # related knowledge with me. Follow me!

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.