Recently, I was working on the Asp.net mail sending function and found a problem: In the vs environment, emails can be sent to the recipient's mailbox. Once deployed on the server, the following message is displayed: failure sending mail error.
After searching for the Internet, the solution is as follows:
1: the SMTP service must be installed on the server in IIS.
2: modify the code smtpclient client = new smtp
The code you are looking for can only send emails without SMTP verification, but now many emails need verification when they are sent. Later, I found the MSDN help and found the code for sending verification, post it to help you!
Copy codeThe Code is as follows: public static int sendmail (string to, string body, string subject)
{
Try
{
Int nContain = 0;
/// Add the sender address
String from = "your EMAIL ";
MailMessage mailMsg = new MailMessage ();
MailMsg. From = new MailAddress (from );
NCon
From is the email address sentTo refers to the destination mailbox being sentSubject is the titleBody is the content that is sent, note that the related words may cause when the Junk mailbox processing{ System.Net.Mail.MailMessage mm = null; SmtpClient Client; Client = new SmtpClient { Host = "smtp.163.com",//qq is smtp.qq.com port
Related namespaces:
Using
System. net;
Using
System. net. mail;
Core Code Example: Protected Void Page_load ( Object Sender, eventargs E) {Mailmessage mm = New Mailmessage ();Mm. to. Add ( " Hooyes@vip.qq.com, lovehooyes@126.com " ); // Recipient addresses, separated by commas Mm. Subject = " Email Subject " ;Mm. Body = " Email content " ;Mm. From = New Mailaddress ( " You@gmail.com " , " Xi Hu Test " );
This exception is returned when an email is sent,
Mailbox unavailable. The server response was: 5.7.1 unable to relay for (email address ).
The solution is to add a sentence
Code :
Client. deliverymethod
=
Smtpdeliverymethod. pickupdirectoryfromiis;
The new Code is as follows: Mailmessage message = New Mailmessage (from, to, subject, body );Smtpclient Client = New Smtpclient ();Client. deli
used to compress all files into ZIP files and save them by mail.
Mail Code:
String smtpserver = "smtp.126.com ";String username = "XXXXXX@126.com ";String userpwd = "XXXX ";String emailfrom = "XXXX@126.com ";
Smtpclient client = new smtpclient (smtpserver );Client. Credentials = new networkcredential (username, userpwd );
Client. deliverymethod = smtpdeliverymethod. Network;
Mailmessage email =
CopyCodeThe Code is as follows: public static void sendemail ()
{
System. net. Mail. smtpclient client = new system. net. Mail. smtpclient ();// Use QQ's email address to send the test. If it is another email address, set it according to POP3/IMAP/SMTP in another email address.
Client. Host = "smtp.qq.com ";Client. usedefacrecredentials = false;Client. Credentials = new system. net. networkcredential (
each mail I want to achieve, and it is not suitable for searching some of the content on the Internet, finally, I decided to modify Jmail by myself.. Net.
Download to a decompilation source code of Jmail. Net (if you need to search for the source code by yourself, we will not provide it here), load VS2005, and start to read the code.
The automatic prompt during code writing shows that the sent code is sent by Dimac. jmail. smtp. send () to directly view Dimac. JMail. smtp. cs code. Locate the m
Reference namespaces:Using System.Net.Mail;Send HTML mail with cc and BCCNeed to be configured in the Mailsettings node under System.Net in the Web. config filepublic static void SendMail (String mailto, string mailcc, String mailbcc, String mailsubject, String mailbody){ MailMessage mmessage = new MailMessage (); MailAddress mTo = new MailAddress (mailto); if (MAILCC! = "") {mailaddress MCC = new MailAddress (MAILCC); MMESSAGE.CC.ADD (MCC); } if (mailbcc! = "") {mailaddress mbcc = n
Attachmentspath){Attachfile = new Attachment (path);MYMAIL.ATTACHMENTS.ADD (Attachfile);}}}catch (Exception err){throw new Exception ("error when adding an attachment:" + err);}SmtpClient smtp = new SmtpClient ();Specify the sender's e-mail address and password to verify sender identitySmtp. Credentials = new System.Net.NetworkCredential (Mailfrom, mailpwd);Setting up an SMTP mail serverSmtp. host = host;T
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.