. Net sends an email

Source: Internet
Author: User
Tags configuration settings mailmessage smtpclient

This articleArticleThis article describes how to configure mail sending through the configuration file (Web. config) and how to send emails according to the configuration.

Applicable framework: Asp.net Framework 2.0/. Net framework3.0/. Net framework3.5

Set the network connection of. NET Framework in Web. config

 

XML/htmlCode
<System.net>
</System.net>
Configure stmp mail sending as follows:

 

XML/html code
// Parent element: configuration (configuration of all namespaces is provided)
<System.net>
// You can add the following element settings.
// Authenticationmodules: Set the module used to verify Web requests
// Connectionmanagement: sets the maximum number of connections to the web server.
// Defaultproxy: sets the HTTP proxy server.
// Mailsettings: Configure SMTP
// Requestcaching: controls the network request Cache Mechanism
// Settings: Configure basic network options for system. net
// <Webrequestmodules> element (network settings): Specify the module's request information from the web server.
<Mailsettings>
// Set the mail sending method in deliverymethod, which is in the network format
<SMTP deliverymethod = "network" from = "xxx@yyy.com">
// Host email sending server
// Username used for identity authentication when sending an email
// Password for verification as follows
<Network host = "smtp.yyy.com" username = "xxx@yyy.com" Password = "zzzzzzz"/>
</SMTP>
</Mailsettings>
</System.net>

Obtain SMTP configuration in Web. config programmatically

Netsectiongroup class

Namespace
System. net. Configuration: For the applicationProgramProvides a class for accessing and updating settings in the configuration file system. Net namespace programmatically.
Assembly
System

Definition:
Public sealed class netsectiongroup: configurationsectiongroup

Note:
This class provides program-based access to information stored in the configuration file.
This class corresponds to the system.net element (network settings) in the network settings document.
This section provides configuration settings for the following namespaces:
System. net
System. net. Cache
System. net. Mail

Read configuration/system.net/mailsettings/stmpconfiguration in the program

 

C # code
// Netsectiongroup has already been introduced above
// Getsectiongroup obtains the configuration of system.net from the specified configuration file
// Webconfigurationmanager. openwebconfiguration
// Open web. config in the root directory of the application to generate the system. configuration. Configuration object instance.
Smtpsection CFG = netsectiongroup. getsectiongroup (webconfigurationmanager. openwebconfiguration ("~ /Web. config "). mailsettings. SMTP;
 

Send email

C # code
Try {
// Instantiate an email message object
Mailmessage email = new mailmessage (CFG. From, mailto );
Email. isbodyhtml = true;
Email. Body = "content of the email to be sent. The preceding settings support HTML content. In addition, you can specify the email. bodyencoding attribute to set the encoding of the email content ";
Email. Subject = "email subject, you can specify the email. subjectencoding attribute to set the email subject encoding ";
// Instantiate an SMTP client object for sending emails
System. net. Mail. smtpclient stmp = new smtpclient (CFG. Network. HOST );
// Set whether to send the message and whether authentication is required. If the following credentials is not required
Stmp. usedefacrecredentials = true;
Stmp. Credentials = new system. net. networkcredential (CFG. Network. username, CFG. Network. Password );
// Send an email
Stmp. Send (email );
}
Catch (exception ex)
{
}

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.