Now you don't need the account + password of the mailbox , now use the account + third-party authorization code (in the mailbox settings)
First of all, the sender needs to open the stmp and pop3 services in the qq mailbox (the first two anyway)
First come first: relatively simple
1 SmtpClient client = new SmtpClient( " smtp.qq.com " ); 2 client.EnableSsl = true ; 3 client.UseDefaultCredentials = false ; 4 client.Credentials = new System.Net.NetworkCredential( " Sender Email Address " , " Third-party authorization code " ); 5 MailAddress from = new MailAddress( " Sender's email address " , " Express from the second uncle ", Encoding.UTF8); // Initialize the sender 6 MailAddress to = new MailAddress( " Recipient's email address " , "" , Encoding.UTF8); // Initialize the recipient; the recipient is not divided into qq or 136 Mailbox 7 // Set the mail content 8 MailMessage message = new MailMessage( from , to); 9 message.Body = " Mail content " ; 10 message.BodyEncoding = System.Text.Encoding.UTF8; 11 message.Subject = " Title " ; 12 message.SubjectEncoding = System.Text.Encoding.UTF8; 13 message.IsBodyHtml = false; 14 15 //发送邮件 16 try 17 { 18 client.Send(message); 19 } 20 catch (InvalidOperationException iex) 21 { } 22 catch (Exception ex) 23 { }
Another one, you can add multiple recipients and cc's
Email email = new Email(); email.mailFrom = "Sender's Address"; email.mailSubject = "Mail Subject"; email.mailBody = "Email Body"; email.isbodyHtml = true; //Is it HTML email.host = "smtp.qq.com";//If it is a QQ mailbox: smtp:qq.com, and so on email.mailToArray = new string[] { "[email protected]", "[email protected]" };//Receiver mail collection email.mailCcArray = new string[] { "[email protected]" };//Cc mail collection if (email.Send()) { Response.Write("<script type='text/javascript'>alert('Successful sending!');history.go(-1)</script>");//If the sending is successful, it will prompt to return to the current page; } else { Response.Write("<script type='text/javascript'>alert('发送失败!');history.go(-1)</script>"); } ----------------------------------------------------------------------------- Here is the packaged class: public class Email { /// <summary> /// sender /// </summary> public string mailFrom { get; set; } /// <summary> /// recipient /// </summary> public string[] mailToArray { get; set; } /// <summary> /// CC /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// title /// </summary> public string mailSubject { get; set; } /// <summary> /// text /// </summary> public string mailBody { get; set; } /// <summary> /// SMTP mail server /// </summary> public string host { get; set; } /// <summary> /// Whether the body is in html format /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// Appendix /// </summary> public string[] attachmentsPath { get; set; } public bool Send() { //Initialize the MailAddress instance with the specified email address MailAddress maddr = new MailAddress(mailFrom); //Initialize the MailMessage instance MailMessage myMail = new MailMessage(); //Add email address to recipient address collection if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //Add email addresses to the CC recipient address collection if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } // sender address myMail.From = maddr; // title of the email myMail.Subject = mailSubject; // The encoding used for the subject content of the email myMail.SubjectEncoding = Encoding.UTF8; // email body myMail.Body = mailBody; //Encoding of the email body myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //Add attachments if there are attachments try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("Error adding attachment:" + err); } SmtpClient client = new SmtpClient(); //Set up the SMTP mail server -- look at the following three sequences: UseDefaultCredentials will cause an error in the order client.Host = host; client.EnableSsl = true; client.UseDefaultCredentials = false; // Specify the sender's email address and password to verify the sender's identity client.Credentials = new System.Net.NetworkCredential(mailFrom, "Authorization Code in Email Settings"); //client.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); try { //Send mail to SMTP mail server client.Send(myMail); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } }
1 /// <summary> 2 /// Core code for sending mail 3 /// </summary> 4 /// <param name="oFFromMail"> Sending email full address </param> 5 /// <param name ="oFFromMailPwd"> Send Mailbox </param> 6 /// <param name="oFDisName"> The associated display name associated with the mailbox </param> 7 /// <param name="toMail"> Recipient mailbox Full address </param> 8 /// <param name="oFSubject"> Subject </param> 9 /// <param name="oFBody">Email content </param> 10 /// <returns> Return whether the sending is successful </returns> 11 private void sendMailMethod ( string oFBody) 12 { 13 try 14 { 15 16 string FromMail = " [email protected] " ; // [email protected] 17 string toMail = " [email protected] " ; // Accepted email address [email protected] 18 // 1: Set server 19 SmtpClient sendmsg = new SmtpClient( " smtp.qq.com"); 20 sendmsg.EnableSsl = true; 21 sendmsg.UseDefaultCredentials = false; 22 sendmsg.Credentials = new System.Net.NetworkCredential("发送人@qq.com", "授权码"); 23 MailAddress from = new MailAddress(FromMail, "FromProzkb", Encoding.UTF8);//初始化发件人 24 MailAddress to = new MailAddress(toMail, "" , Encoding.UTF8); // Initialize recipient 25 // Set up mail 26 MailMessage mailmsg = new MailMessage( from ,to); 27 mailmsg.Subject = " OjbkTitle " ; 28 mailmsg.Body = " Hello , This is the content of a test mail!<br /> " + oFBody; 29 mailmsg.BodyEncoding = System.Text.Encoding.UTF8; 30 mailmsg.IsBodyHtml = true ; 31 32 sendmsg.Send(mailmsg); 33 34 } 35 catch (Exception ex) 36 { 37 throw new Exception(ex.Message, ex); 38 } 39 }
These codes are partially modified from the Internet;
So now it's back to the network...
Sleepy. . . . . . have to work tomorrow. . . . .