. NET System.Web.Mail sending mail (set sender to display only user name)

Source: Internet
Author: User
Tags mailmessage net send

http://blog.163.com/hao_2468/blog/static/130881568201141251642215/. NET System.Web.Mail sending mail

2011-05-12 17:16:42|  Category: ASP-Learning | Tags:. net send mail | Report | Font size Subscription

NET System.Web.Mail sending mail

Use System.Web.Mail to send mail for. net1.1. net2.0 Please use System.Net.Mail

Refer to System.Web first

1. Send a simple message

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. BODY = "This is my test email body";

Smtpmail.smtpserver = "localhost"; Your real server goes here

Smtpmail.send (mail);

Here the SmtpServer can only be those who do not need to verify the SMTP server, such as 126,sina,yahoo and so on the mailbox, all need to verify, so it is not available. Using these mailboxes, we'll talk about them later.

2, send HTML mail

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. BodyFormat = mailformat.html;

Mail. Body = "This is my test e-mail body.<br><b>this part was in bold</b>";

Smtpmail.smtpserver = "localhost"; Your real server goes here

Smtpmail.send (mail);

3, send attachment

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. BODY = "This is my test email body.";

MailAttachment attachment = new MailAttachment (Server.MapPath ("test.txt")); Create the attachment

Mail. Attachments.Add (attachment); Add the attachment

Smtpmail.smtpserver = "localhost"; Your real server goes here

Smtpmail.send (mail);

4. Modify the sender and recipient names

For example, the sender's address is [email protected], we use Outlook to receive the letter, from a column will be displayed directly [email protected]

Can you show a friendly name in the From column?

Like showing Tony Gong.

Here's how:

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "\" john\ "<[email protected]>";

Mail. from = "\" Tony gong\ "<[email protected]>";

Mail. Subject = "This is a test email.";

Mail. BODY = "This is my test email body.";

Smtpmail.smtpserver = "localhost"; Your real server goes here

Smtpmail.send (mail);

5, sent to multiple people

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]; [Email protected]; [email protected] ";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. BODY = "This is my test email body.";

Smtpmail.smtpserver = "localhost"; Your real server goes here

Smtpmail.send (mail);

6. Send a letter to the mailbox that requires SMTP authentication

To prevent spam, the vast majority of SMTP servers need to verify

The sending method is as follows:

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. Body = "Some text goes Here";

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); Basic Authentication

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); Set your username here

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your password"); Set your password here

Smtpmail.smtpserver = "smtp.126.com"; Your real server goes here

Smtpmail.send (mail);

7. Modify the port of the SMTP server and use SSL encryption

Most SMTP servers have ports of 25, but some are not

At the same time, most SMTP servers do not require SSL login, while others require

For example, the GMAIL,SMTP port is: 465, while supporting SSL

The code is as follows:

[C #]

MailMessage mail = new MailMessage ();

Mail. to = "[email protected]";

Mail. from = "[email protected]";

Mail. Subject = "This is a test email.";

Mail. Body = "Some text goes Here";

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); Basic Authentication

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); Set your username here

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your password"); Set your password here

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);

Mail. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

Smtpmail.smtpserver = "smtp.126.com"; Your real server goes here

Smtpmail.send (mail);

. NET System.Web.Mail sending mail (set sender to display only user name)

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.