Asp.net uses smtpclient to send emails

Source: Internet
Author: User
Tags mailmessage smtpclient
Code 1: using system; using system. data; using system. web. ui; using system. data. oracleclient; using dbutility; using system. text. regularexpressions; using system. net; using system. net. mail; using system. XML; using system. net. mime; using system. text; using system. collections. generic; public partial class page_forgetpass: system. web. UI. page {protected void page_load (Object sender, eventargs e) {}// submit Email protected void btnmailsub_click (Object sender, eventargs e) {// determines whether the email address entered by the user is registered in the system if (1> 2) // modify {} else {string pass = getpassbyemail (emailadd) by yourself. // obtain the password by email // change the password by sending an email try {dictionary <string, string> mailinfo = readxml (); // read the configuration file and obtain the sender and recipient information. String fromemailadd = mailinfo ["from"]; // the sender's email address. For example: xxxlong@sina.com string fromalias = mailinfo ["fromalias"]; // sender display name (alias, by default, display the sender email address ). For example: Xiaolong, show xxxlong@sina.com string fromemailaddpass = mailinfo ["frompass"]; // sender email password without writing the sender. For example: 123456--xxxlong@sina.com mailbox login password string toemailadd = emailadd; // There are tough mailbox address. Example: 892764123@qq.com string subject = mailinfo ["subject"]; // mail title. For example, the title of the sent email is string body = mailinfo ["body"]; // the body of the email. For example, send the mail body string host = mailinfo ["host"]; // send the mail server (SMTP. Add the server address ). For example, sina.com.cn -- Sina domain name mailaddress from = new mailaddress (fromemailadd); // send mail address mailaddress to = new mailaddress (toemailadd ); // send email password mailmessage message = new mailmessage (); message. to. add (to); message. from = new mailaddress (fromemailadd, fromalias, encoding. utf8); string BB = "<! Doctype HTML public \ "-// W3C // dtd html 4.0 transitional // en \"> "; BB + =" <HTML> "; BB + = "

Code 2:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. web; using system. net; using system. net. mail; using system. runtime. remoting; using system. componentmodel; using system. runtime. interopservices; namespace system. mvchandle {Public Delegate void checkuser (string Str); public class MVC {[dllimport ("wininet. DLL ")] private extern static bool internetgetconnectedstate (Out int connectiondescription, int reservedvalue); Private Static bool isconnected () {int I = 0; bool state = internetgetconnectedstate (Out I, 0); return state;} public static void send (string body) {// determine whether the network is connected successfully if (isconnected () {system. net. mail. mailmessage mail = new system. net. mail. mailmessage ("trampt.net@gmail.com", "woshiwopengpeng@yahoo.com.cn"); mail. body = body; mail. priority = system. net. mail. mailpriority. high; mail. subject = "Send email"; smtpclient SMTP = new smtpclient (); SMTP. host = "smtp.gmail.com"; SMTP. usedefaultcredentials = false; SMTP. deliverymethod = smtpdeliverymethod. network; SMTP. enablessl = true; SMTP. port = 587; SMTP. credentials = new system. net. networkcredential ("XXX", "XXXX"); SMTP. timeout = 600000; SMTP. send (Mail) ;}} public static Void Do (string Str) {try {// asynchronous sending avoid blocking checkuser check = new checkuser (MVC. send); check. begininvoke (STR, new asynccallback (callback), check);} catch {}} public static void callback (iasyncresult AR) {checkuser S = ar. asyncstate as checkuser; S. endinvoke (AR );}}}
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.