Use. NET built-in smtpclient to send mail

Source: Internet
Author: User
Tags imap email account mailmessage smtpclient

  Objective  

There are many ways to send mail, such as sending mail via the. Net Framework's built-in SmtpClient, using an open source project Lumisoft.net, and so on, and here's how to use SmtpClient under the System.Net.Mail namespace to send mail.

  First, the Mail sending process

  For example, a use 163 mailbox to send mail to B (QQ mailbox). First, a will mail through the SMTP (Simple Mail Transfer Protocol) protocol to the 163 SMTP server, 163 of the SMTP server according to B's mailbox account, mail through the SMTP protocol to the SMTP server mailbox. When the SMTP server receives the mail message, it will store it on the mail storage device of the QQ mailbox. When B logs into the QQ mailbox, if there is a new message, the POP3 server will read the mail from the mail storage device on the QQ mailbox and send it through the Pop3/imap service to the mail client's B.

Schematic diagram

  

  Second, the predecessor work

1. Prepare two test mailboxes (use QQ mail here)

2. Open the POP3/SMTP service and IMAP/SMTP service for the mailbox (QQ mailbox needs to be opened under settings---account)

  

 Iii. beginning of coding

1. Create a MailMessage object to edit the message body and indicate the sender and receiver

2. Create the SmtpClient object to send the message, you need to indicate the sender's account number and password (QQ mailbox to use authorization code)

3. Sample Code

  

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Net;5 usingSystem.Net.Mail;6 usingsystem.security;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 Ten namespaceMailTest One { A     class Program -     { -         Static voidMain (string[] args) the         { -             stringFrommail ="[email protected]"; -             stringTomail ="[email protected]"; -MailMessage MailMessage =NewMailMessage +             { -                 //Sender +from =Newmailaddress (Frommail) A             }; at  -             //recipients can add multiple recipients -MAILMESSAGE.TO.ADD (Newmailaddress (Tomail)); -  -             //mailmessage.cc Gets the address collection that contains the CC (CC) recipients for this e-mail message -             //Message Subject inmailmessage.subjectencoding=Encoding.UTF8; -Mailmessage.subject ="Hello"; to  +             //message body -Mailmessage.bodyencoding =Encoding.UTF8; theMailmessage.body ="<p style= ' color:red;font-size:14px; ' > Mail Test </p>"; *              $             //If you want to send HTML-formatted messages, you need to set this propertyPanax Notoginsengmailmessage.isbodyhtml =true; -  the             //The message content is displayed in the message body as a picture +             //need to indicate src= ' Cid:idname (resource ID) for the picture AAlternateView htmlBody = alternateview.createalternateviewfromstring ("",NULL,"text/html"); the  +             //then add the absolute address of the file in Linkedresource, and contenttype for example image/gif,text/html ... Consistent with contenttype in the response message of the HTTP request -Linkedresource LR =NewLinkedresource ("1.gif","Image/gif"); $  $             //bind the idname specified above -Lr. ContentId ="ZFP"; -  the             //add a linked resource - HTMLBODY.LINKEDRESOURCES.ADD (LR);Wuyi  the MailMessage.AlternateViews.Add (htmlBody); -  Wu             //send an attachment to indicate the absolute address of the attachment -Attachment Attachment =NewAttachment ("1.txt"); About mailMessage.Attachments.Add (attachment); $  -  -             //Create a mail sending client -             Try A             { +                 //use QQ mailbox here to open POP3/SMTP service and IMAP/SMTP service under the account set-up the                 //QQ Mailbox of the outgoing server smtp.qq.com port -SmtpClient sendclient =NewSmtpClient ("smtp.qq.com", -) $                 { the                     //Specify your email account and password the                     //when a third-party client logs on to the QQ mailbox, the password is the authorization code the                     //login QQ Mailbox in the settings----account can generate authorization code theCredentials =NewNetworkCredential (Frommail,"xmxyldrmtvnrddfe") -                 }; in  the                 //specify how to send e-mail theSendclient.deliverymethod =smtpdeliverymethod.network; About  the                 //Specify a link that uses SSL encryption using Secure Sockets the                 theSendclient.enablessl =true; + sendclient.send (mailmessage); -             } the             CatchBayi             { the                 Throw; the             } -             -  theConsole.WriteLine ("ok!"); the Console.readkey (); the  the         } -     } the}
View Code

  Iv. anomalies that may occur

1.system.net.mail.smtpexception: "The operation has timed out. ”

This exception could be an error in the IP and port of the mail server

2.system.net.mail.smtpexception: "There is a syntax error in the parameter or variable. The server responds as: Mail from address must is same as authorization user "

This exception is usually the email sender's account and password settings error, here again to remind you, QQ mailbox password need to use authorization code

 

Use. NET built-in smtpclient to send 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.