Small solution for sending emails in. Net 2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

I remember it was in. net 1. the code for writing emails in X is to use the CDO in the operating system's CDO component. message to implement, because this can implement the SMTP server authentication and other detailed functions of sending a series of emails, but no matter what the error will prompt is CDO. the Message object cannot be accessed or other CDO. message exception. net Framework 1. system. web. mail. the SmtpMail class provides very Simple mail sending functions, even the SMTP server authentication function is not provided, it is really dizzy.

However, in. Net Framework 2.0, the first line of the SDK description for System. Web. Mail. SmtpMail has clearly marked the red bold Chinese character "NOTE: This class is outdated now .", It seems that this class is indeed a. Net Framework 1. X design problem, it may also be an excessive class.

In. Net Framework 2.0, it is replaced by the class in the System. Net. Mail namespace. Its Mail sending function has been designed to be Very good. My practice is like going deeper and later. Let's take a look at how to use code to send the simplest mail (no SMTP authentication required) in. Net 2.0 ).

String mailServerName = "mail.powerise.com.cn ";
String from = "pcmax@powerise.com.cn ";
String to = "pcmax@etang.com ";
String subject = "test ";
String body = "hello ";
Try
{
// MailMessage indicates the email
Using (MailMessage message = new MailMessage (from, to, subject, body ))
{
// SmtpClient is the body of the sent mail. This constructor informs SmtpClient which SMTP server is used to send the mail.
System. Net. Mail. SmtpClient mailClient = new System. Net. Mail. SmtpClient (mailServerName );
MailClient. usedefacrecredentials = true;
// Final sending Method
MailClient. Send (message );
}
Loger. Write ("Message sent .");
}
Catch (Exception ex)
{
Loger. Write (ex. Message );
}

This Code cannot be used in practice, because the current mail server requires SMTP server authentication.

Now I will introduce the code for sending emails using SMTP Server Authentication:

String mailServerName = "smtp.21cn.com ";
String from = "amax@21cn.com ";
String to = "amax@21cn.com ";
String subject = "test ";
String body = "hello ";
Using (MailMessage message = new MailMessage (from, to, subject, body ))
{
// SmtpClient is the body of the sent mail. This constructor informs SmtpClient which SMTP server is used to send the mail.
System. Net. Mail. SmtpClient mailClient = new System. Net. Mail. SmtpClient (mailServerName );
// Construct an authentication instance
System. Net. NetworkCredential nc = new System. Net. NetworkCredential ("amax@21cn.com ","******");
// Assign the authentication instance to mailClient
MailClient. Credentials = nc;
// Do not add the value of mailClient. usedefacrecredentials under the "mailclient. Credentials = nc;" statement. Whether it is false or true, it will cause program running errors.

// Final sending Method
MailClient. Send (message );
}

Currently, you can see that all emails are sent by Code, while Asp. in the Framework of Net2.0, more configuration is considered, that is, application control is implemented through config, rather than hard coding the vast majority of parameters, after reading the SDK, you can see the configuration section of mailSettings. In the SDK, the configuration section marked "This attribute is in. ". This configuration section belongs to the <system.net> section.

Next we will use some configuration information to obtain the above Code, and we will immediately know how concise your Code is.
Config:

<MailSettings>
<Smtp from = "amax@21cn.com">
<Network host = "mail.21cn.com" password = "**********" port = "25" userName = "amax" defaultCredentials = "false"/>
</Smtp>
</MailSettings>

Code:

String subject = "test ";
String body = "hello ";
SmtpSection smtpSec = (SmtpSection) ConfigurationManager. GetSection ("system.net/mailSettings/smtp ");
Using (MailMessage message = new MailMessage (smtpSec. From, "amax@21cn.com", subject, body ))
{
System. Net. Mail. SmtpClient mailClient = new System. Net. Mail. SmtpClient ();
MailClient. Send (message );
}

Ha, is it a sense of accomplishment. In fact, as long as you read the SDK, you will get a lot of tips on sending emails. Today, I wrote this article, which was supposed to be completed in March, because the company's project is too tight, every day, I am overwhelmed, So I continue to write it today. I hope that my friends who have traveled through this detour will forgive me.

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.