. NET Sending mail

Source: Internet
Author: User
Tags mailmessage smtpclient

First add async= "true" to the header of the page


Introduced in the background code:
Using System.Net;
Using System.Net.Mail;
Using System.Text;

public void SendMail (string addrto, String mailbody)
{
String addrfrom = "****** @qq. com";//Mailbox ID
String Pwdfrom = "* * *";//Mailbox password
Setting SMTP Server information
SmtpClient smtpclient = new SmtpClient ();
Smtpclient.host = "smtp.qq.com";//Specify SMTP service name such as QQ mailbox for smtp.qq.com Sina cn mailbox for smtp.sina.cn, etc.
Smtpclient.port = 25; Specify port number


Create Server Authentication
NetworkCredential networkcredential_my = new NetworkCredential (Addrfrom, Pwdfrom);
Instantiate sender Address
MailAddress mailaddress_from = new MailAddress (Addrfrom, "*******");
Specify sender information including email address and mailbox password
Smtpclient.credentials = new NetworkCredential (mailaddress_from. Address, Pwdfrom);

MailAddress mailaddress_to = new MailAddress (Addrto);

MailMessage mailmsg = new MailMessage (Mailaddress_from, mailaddress_to);

MailMsg. To.clear (); Clear history send messages to prevent recipients from receiving error messages when they are sent (the list of recipients repeats repeatedly)
MailMsg. From = mailaddress_from;//Sender mailbox
MailMsg. To.add (mailaddress_to);//Add Recipient email Address
Message subject
MailMsg. Subject = "password";
MailMsg. subjectencoding = Encoding.UTF8;
Message body
MailMsg. Body = Mailbody;
MailMsg. bodyencoding = Encoding.UTF8;

smtpclient.sendcompleted + = new Sendcompletedeventhandler (smtpclient_sendcompleted);//processing events after the mail is sent
Object userState = MailMsg;

Smtpclient.sendasync (MailMsg, userState);
}

public void smtpclient_sendcompleted (object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{


ShowMessage ("Sent successfully! ");
Confirmbttn.enabled = true;
Try
{
if (e.cancelled)
{
ShowMessage ("Send Cancel");
}
if (E.error = = null)
{
ShowMessage ("Password has been sent to your registered email");

}
Else
{
ShowMessage ("Send failed" + e.error.message);
}
}
Catch
{

}
throw new NotImplementedException ();
}

. NET Sending mail

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.