In C #. NET Environment send mail (under the web)

Source: Internet
Author: User
Tags mail mailmessage
web| Send mail

at Microsoft. NET environment in the System.Web.Mail namespace, provides a number of classes, so that you do not need to understand POP3, such as a series of network protocols, you can easily in the program to send mail, very simple, this article on a brief introduction, how to use the C # language to achieve.

First, we're going to add this namespace

Using System.Web.Mail;

Now we're going to generate the message itself. A class named MailMessage is provided here. An example of this class is the message object that we want to send.

MailMessage message=new MailMessage ();

Construct the message that we want to send by setting the properties of the Mesasge object.

The sender address, which is a string type
Message.from= "";

Recipient address, is a string type "";

The address of the CC message, which is the string type "";

Message subject, is a string type
Message.subject= "This is an example of a test";

Message content, is a string type
Message.body= "This is an example of a test email";

Message type
Message.bodyformat=mailformat.text; Text type

/* You can also set the following as a hypertext type
message.bodyformat=mailformat.html; Hypertext Type * *

Set the priority of a message

/* can also be set as follows
Message.priority=mailpriority.normal; General priority
Message.priority=mailpriority.high; High-Priority * *

At this point, the message we want to send is basically constructed.
Now, we need to use another class smtpmail in this space to send this message.
Before using this class, we first set a property of it.

Set up the mail server, if not set will be your system default mail server to send
This is a static (static) property, so you do not need to instantiate the class
This is also a string type

Smtpmail.smtpserver= "";

Now, we can send this email.

Send mail
This is a static (static) property, so you do not need to instantiate the class

Smtpmail.send (message);

If all goes well, this email should have been sent out. The email address, server address, etc. listed in this example are virtual, and you can change the test to what you need.

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: 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.