asp.net C # supports HTML format, with attachments, Chinese senders, bcc, cc-Mail Bulk code

Source: Internet
Author: User
Tags mailmessage smtpclient

ASP tutorials. NET C # supports HTML format, with attachments, Chinese senders, bcc, cc-Mail Bulk code

You can set this in Web.config

<system.net>

<mailsettings>
<smtp>
<network host= "hostname" port= "username=" a@163.com "xxxx" password=
</smtp>
</mailsettings>
</system.net>



Attachment

String serverfilename = "";
if (this.upfile.postedfile.contentlength!= 0)
{
string upfilename = This.upfile.postedfile.filename;
string[] strtemp = Upfilename.split ('. ');
String upfileexp = Strtemp[strtemp.length-1].tostring ();
Serverfilename = Server.MapPath (datetime.now.tostring ("YYYYMMDDHHMMSS") + "." + Upfileexp);
This.upfile.postedfile.saveas (Serverfilename);
Mm.attachments.add (New MailAttachment (Serverfilename));
}

asp.net tutorial mail Send code

Introduction of namespaces
Using System.Net;
Using System.Net.Mail;
SmtpClient smtp = new SmtpClient (); Instantiate a SmtpClient
Smtp.deliverymethod = Smtpdeliverymethod.network; Set SMTP Outbound to Network
Smtp.enablessl = FALSE;//SMTP Whether SSL encryption is enabled on the server
Smtp.host = "smtp.163.com"; Specify SMTP server address
Smtp.port = 25; Specifies the port of the SMTP server, the default is 25, and if the default port is used, you can omit
If your SMTP server does not need authentication, use the following method, however, there is no need for authentication at present.
Smtp.usedefaultcredentials = true;
If authentication is required, use the following method
Smtp.credentials = new NetworkCredential ("Mailbox Account @163.com", "Mailbox Password");
MailMessage mm = new MailMessage (); Instantiate a message Class
mm.priority = Mailpriority.high; Message priority, divided into low, normal, high, usually with normal
Mm.from = new MailAddress ("Mailbox Account @163.com", "Really interesting", encoding.getencoding (936));
The source of the message that the receiving party sees;
The first parameter is the sender's e-mail address.
The second parameter is the name displayed by the sender
The third parameter is the encoding used by the second parameter, and if the designation is incorrect, the opponent will receive a garbled display
936 is the codepage value of Simplified Chinese
Note: The above mail source, must and you log in the mailbox of the same account, or will fail to authenticate
Mm.replyto = new MailAddress ("test_box@gmail.com", "My Receive Mailbox", Encoding.GetEncoding (936));
ReplyTo represents the default receiving address when replying to a message, that is: you send a letter with a mailbox, but use another to receive
The meaning of the latter two parameters, with the meaning of the
Mm.cc.add ("a@163.com,b@163.com,c@163.com");
CC, support mass, multiple e-mail addresses separated by a half-width comma
Of course, you can also use the full address as follows:
Mm.cc.add (New MailAddress ("A@163.com", "cc A", encoding.getencoding (936));
Mm.cc.add (New MailAddress ("B@163.com", "cc B", encoding.getencoding (936));
Mm.cc.add (New MailAddress ("C@163.com", "CC C", encoding.getencoding (936));
Mm.bcc.add ("d@163.com,e@163.com");
The secret sender of the message, support mass, multiple e-mail addresses separated by a half-width comma
Of course, you can also use the full address as follows:
Mm.cc.add (New MailAddress ("d@163.com", "Dense transmitter D", encoding.getencoding (936));
Mm.cc.add (New MailAddress ("e@163.com", "dense send E", encoding.getencoding (936)));
Mm.sender = new MailAddress ("xxx@xxx.com", "Mail Sender", encoding.getencoding (936));
Can be set arbitrarily, this information is included in the message header, but does not verify the validity and is not displayed to the recipient
To tell you the truth, I don't know what the real effect is, we can ignore it and not write it.
Mm.to.add ("g@163.com,h@163.com");
The recipient of the message, support mass, separated by a half-width comma between multiple addresses
Of course, it can be added with full address
Mm.to.add (New MailAddress ("g@163.com", "Receiver G", encoding.getencoding (936));
Mm.to.add (New MailAddress ("h@163.com", "Receiver H", encoding.getencoding (936));
Mm.subject = "This is the mail title"; Message headers
mm.subjectencoding = encoding.getencoding (936);
Here is very important, if your mail title contains Chinese, here must be specified, or the other side received very likely garbled.
936 is the pagecode of Simplified Chinese, if it is English title, this sentence can be ignored
Mm.isbodyhtml = true; Whether the message body is HTML format
mm.bodyencoding = encoding.getencoding (936);
The message body code, set incorrectly, the receiver will receive garbled
Mm.body = "<font color=" Red "> Email 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, you can do without specifying
You can add multiple attachments
Mm.attachments.add (The new Attachment (@ "D:b.doc"));
Smtp.send (mm);

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.