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!