20 secrets of mvc3-(5) Send a welcome email

Source: Internet
Author: User
Tags mailmessage smtpclient

Scenario
Many websites require people to register before accessing the content or making comments. How can a website such as Niu Mao make people remember every website they have registered. During the registration process, you can send an email to remind users that they have just registered. In this way, they may return to your website later.

Solution

After the user registers, use smtpclient and mailmessage to send email notifications.

Discussion

Before sending an email, you need to configure an SMTP server, port, user name, and password. To simplify the configuration, we recommend that you configure it in the appsetting of Web. config.

 <  Appsettings  > 

< Add Key = "Webpages: version" Value = "1.0.0.0" />

< Add Key = "Clientvalidationenabled" Value = "True" />

< Add Key = "Unobtrusivejavascriptenabled" Value = "True" />

< Add Key = "Smtpserver" Value = "Localhost" />

< Add Key = "Smtpport" Value = "25" />

< Add Key = "Smtpuser" Value = "" />

< Add Key = "Smtppass" Value = "" />

< Add Key = "Adminemail" Value = "No-reply@no-reply.com" />

</ Appsettings >

If necessary, update these values to reflect your SMTP server, port, username, and password.

Prompt: You can also use the ASP. NET Configuration tool of Visual Studio to configure.

Select ApplicationProgram-> Configure SMTP email settings

To facilitate the organization of the project structure, we need to create a new folder and a new class to include the necessary email sending function.

Right-click the project and choose add> Create folder and name it uitls. Right-click a new class and name it mailclient. CS.

The mailclient class and its functions are defined as static for ease of use. In the future, when he is integrated into a new function, he does not need to create a new instance for it. Below is a complete mailclient class:

 Using System;
Using System. Collections. Generic;
Using System. configuration;
Using System. LINQ;
Using System. net;
Using System. net. mail;
Using System. Web;

Namespace Mvcapplication. utils
{

Public Static Class Mailclient
{
Private Static Readonly Smtpclient client;
Static Mailclient ()
{
Client = New Smtpclient
{
Host =
Configurationmanager. deleettings [" Smtpserver " ],
Port =
Convert. toint32 (
Configurationmanager. deleettings [ " Smtpport " ]),
Deliverymethod = smtpdeliverymethod. Network
};
Client. usedefacrecredentials = False ;
Client. Credentials = New Networkcredential (
Configurationmanager. deleettings [ " Smtpuser " ],
Configurationmanager. deleettings [ " Smtppass " ]);
}
Private Static Bool Sendmessage ( String From , String To,
String Subject, String Body)
{
Mailmessage Mm = Null ;
Bool Issent = False ;
Try
{
// Create our message
Mm = New Mailmessage (From , To, subject, body );
Mm. deliverynotificationoptions =
Deliverynotificationoptions. onfailure;
// Send it
Client. Send (mm );
Issent = True ;
}
// Catch any errors, these shoshould be logged and
// Dealt with later
Catch (Exception ex)
{
// If you wish to log email errors,
// Add it here...
VaR Exmsg = ex. message;
}
Finally
{
Mm. Dispose ();
}
Return Issent;
}
Public Static Bool Sendwelcome ( String Email)
{
String Body = " Put welcome email content here... " ;
Return Sendmessage (
Configurationmanager. deleettings [ " Adminemail " ],
Email, " Welcome Message " , Body );
}
}
}

First, create a new smtpclient instance through webconfig configuration. Then create a sendmessage function. This function is private and should not be called directly. This function is called when it is actually executed and sent. It creates a new mailmessage object and sends it through the smtpclient object built by the front-end vertex. The sendwelcome function is used to create an email Receiving address. It generates a common message to send your email. It is sent through the sendmessage function.

To send email notifications after registration. The register action in account controller must call the sendwelcome method after the user successfully creates an account.

[Httppost]
Public Actionresult register (registermodel Model)
{
If (Modelstate. isvalid)
{
// Attempt to register the user
Membershipcreatestatus createstatus;
Membership. createuser (model. username, model. Password, model. email, Null , Null , True , Null , Out Createstatus );

If (Createstatus = membershipcreatestatus. Success)
{
Mailclient. sendwelcome (model. Email );
Formsauthentication. setauthcookie (model. username, False /* Createpersistentcookie */ );
Return Redirecttoaction ( " Index " , " Home " );
}
Else
{
Modelstate. addmodelerror ( "" , Errorcodetostring (createstatus ));
}
}

// If we got this far, something failed, redisplay form
Return View (model );
}

ForwardCodeIs a basic example. In today's society, it is a good idea to process existing applications in an automated manner.

You can further extend this example. A verification message is included in your welcome email. This will verify the validity of your email address. Ask him to click the link in the welcome email. Then he can log on.

For more information, see

Smtpclient and mailmessage

Translator's note: In. Net 4.0, Microsoft has provided a new helper "webmail ". More convenient.

If you are interested, you can check webmail.

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.