. NET e-mail "Go"

Source: Internet
Author: User
Tags email account mailmessage smtpclient

For. NET, since 2.0, e-Mail has been a very easy thing to do. Below I give a C # mass mail instance, do a more detailed note, I hope to have a friend need help.

Introducing namespaces
Using system.net;
Using System.Net.Mail;

SmtpClient smtp = new SmtpClient (); Instantiate a SmtpClient
Smtp. Deliverymethod = Smtpdeliverymethod.network; Set the outbound mode of SMTP to Network
Smtp. Enablessl = FALSE;//SMTP Whether SSL encryption is enabled on the server
Smtp. Host = "smtp.163.com"; Specify the SMTP server address
Smtp. Port = +; Specifies the port of the SMTP server, which defaults to 25, and if the default port is used, save
If your SMTP server does not require authentication, use the following method, but there is no need for authentication at this time.
Smtp. useDefaultCredentials = true;
If authentication is required, use the following method
Smtp. Credentials = new NetworkCredential ("email account @163.com", "email password");
MailMessage mm = new MailMessage (); Instantiate a message Class
Mm. priority = Mailpriority.high; Message priority, divided into low, normal, high, usually with normal can
Mm. from = new MailAddress ("email account @163.com", "Really interesting", encoding.getencoding (936));
The source of the message the recipient sees;
The first parameter is the sender's email address.
The second parameter is the name displayed by the sender.
The third parameter is the encoding used by the second parameter, and if it is not specified correctly, the message is garbled after it is received
936 is the codepage value of Simplified Chinese

Note: The above mail source must be consistent with the account you log in to the mailbox, or it will fail to authenticate

Mm. ReplyTo = new MailAddress ("[Email protected]", "My Receive Mailbox", Encoding.GetEncoding (936));
ReplyTo means the default receive address when replying to a message, i.e. you use one mailbox to send it, but you use the other to collect the letter.
Meaning of the above two parameters, with the meaning of the From
Mm. Cc. ADD ("[Email protected],[email protected],[email protected]");
CC of the message, support for mass, multiple email addresses separated by commas in half-width

Of course, you can also use the full address, as follows:
Mm. Cc. ADD (New MailAddress ("[Email protected]", "cc Person A", encoding.getencoding (936)));
Mm. Cc. ADD (New MailAddress ("[Email protected]", "cc person B", encoding.getencoding (936)));
Mm. Cc. ADD (New MailAddress ("[Email protected]", "CC person C", encoding.getencoding (936)));

Mm. Bcc.add ("[Email protected],[email protected]");
Mail The secret sender, support mass, multiple email addresses separated by commas

Of course, you can also use the full address, as follows:
Mm. Cc. ADD (New MailAddress ("[Email protected]", "Bcc D", encoding.getencoding (936)));
Mm. Cc. ADD (New MailAddress ("[Email protected]", "Bcc e", encoding.getencoding (936)));
Mm. Sender = new MailAddress ("[Email protected]", "Mail Sender", encoding.getencoding (936));
Can be set arbitrarily, this information is included in the message header, but is not validated and is not displayed to the recipient
To tell the truth, I do not know what practical role, we can ignore, or do not write this
Mm. To.add ("[Email protected],[email protected]");
Message recipients, support mass, multiple addresses separated by commas

Of course, you can add it with a full address.

Mm. To.add (New MailAddress ("[Email protected]", "Recipient G", encoding.getencoding (936)));
Mm. To.add (New MailAddress ("[Email protected]", "Receiver H", encoding.getencoding (936)));
Mm. Subject = "This is the message header"; Message header
Mm. subjectencoding = encoding.getencoding (936);
It is very important here, if your mail title contains Chinese, it must be specified here, otherwise the other party received the most likely is garbled.
936 is a simplified Chinese pagecode, if the English title, this sentence can be ignored without
Mm. Isbodyhtml = true; Whether the message body is in HTML format

Mm. bodyencoding = encoding.getencoding (936);
message body encoding, incorrect settings, receiver will receive garbled

Mm. Body = "<font color=" Red "> Mail test, hehe </font>";
Message body
Mm. Attachments.Add (New Attachment (@ "D:a.doc", SYSTEM.NET.MIME.MEDIATYPENAMES.APPLICATION.RTF));
Add an attachment, the second parameter, which represents the file type of the attachment, can be used without specifying
Multiple attachments can be added
Mm. Attachments.Add (New Attachment (@ "D:b.doc"));
Smtp. Send (mm); Send the message, and if you do not return an exception, you are done.

. NET e-mail "Go"

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.