APs. Net sends emails

Source: Internet
Author: User
Tags mail account mailmessage smtpclient

In DOTNET, smtpclient is used to send emails. Previously, system. Web. Mail was not recommended. The new method is in the namespace system. net. Mail.

The following namespaces are involved:

Using system. net. mail;
Using system. net;
Using system. net. Mime;

Here are two simple examples:

 

// Synchronously send the smtpclient client = new smtpclient (); // SMTP server address client. host = "mail1.ufida.com"; // SMTP server port client. port = 25; // connect timeout client. timeout = 5000; // mail account client. credentials = new networkcredential ("*** @ ufida.com", "***"); // log on to the account and password of the mailbox // simple send client. send ("*** @ ufida.com", "*** @ QQ.com", "subject", "MSG"); // sender, recipient, subject, body

In this way, a simple mail is sent completely. The client. Send () method is to send the mail synchronously, simply put, it is a single thread.

You can also use the asynchronous mail sending method. When the mail is sent, a sendcompletedeventhandler event is returned.

The send button event is called as follows:

Private void button2_click (Object sender, eventargs e) {// send an email asynchronously // Create mail message mailaddress from = new mailaddress ("*** @ ufida.com", "UF "); mailaddress to = new mailaddress ("*** @ QQ.com", "QQ"); mailmessage mail = new mailmessage (from, ); // Add more to add multiple recipients mailaddress to2 = new mailaddress ("*** @ QQ.com", "QQ"); mail. to. add (to2); // Add more CC add CC mailaddress cc = new mailaddress ("*** @ qq. CO M "," QQ "); mail. cc. add (CC); // Add more BCC add BCC mailaddress BCC = new mailaddress ("*** @ QQ.com", "QQ"); mail. BCC. add (BCC); mail. subject = "subject"; mail. body = "MSG"; mail. bodyencoding = system. text. encoding. utf8; // body encoding format mail. isbodyhtml = false; // whether the body is an HTML email // create attachment by stream reads the attachment and sends it as a file stream // system. io. fileinfo file = new system. io. fileinfo (@ "E: \ mycos_myjob_issue003.pdf"); // attachment ATT = new attachment (file. openread (), mediatypenames. application. PDF); // or add file or directly specify the absolute path of the attachment file // attachment ATT = new attachment (@ "E: \ mycos_myjob_issue003.pdf", mediatypenames. application. octet); // mail. attachments. add (ATT); // create account networkcredential account = new networkcredential ("*** @ ufida.com", "***"); smtpclient client = new smtpclient (); // SMTP server address client. host = "Mai L1.ufida.com "; // whether SSL is SSL-encrypted client. enablessl = false; // SMTP server port client. port = 25; // connect timeout client. timeout = 5000; // mail account client. credentials = Account; client. sendcompleted + = new sendcompletedeventhandler (mailsendcompleted); // register the event response method after the email is sent. // simple send asynchronously sends the client. sendasync (Mail, "testmail"); // The second parameter is passed into the sendcompletedeventhandler event. Therefore, you can put the mail-related information in the second parameter, in this way, after the email is sent To determine which email is sent.}

The following is an event response method for email sending completion.

Public void mailsendcompleted (Object sender, asynccompletedeventargs e) {If (E. cancelled) {label1.text = "cancel";} else if (E. Error! = NULL) {label1.text = "error" + E. error. message;} else {label1.text = "completed" + E. userstate. tostring (); // userstate is client. the second parameter in sendasync () method }}

These mail sending classes are easy to use, so the function is also very simple. For example, if you want to calculate the size of the email before sending the email (usually the size of the body content plus the attachment size), or obtain the progress in sending the email, these functions are not available, or I have not found them, in addition, I personally think that this kind of highly encapsulated class is easy to use, that is, it is not flexible enough.

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.