Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc framework, automatically send e-mail

I want my asp.net mvc framework system to send an e-mail everytime a certain action (inside a certain controller) is fired off. Are there any third party libraries or .net standard ways to accomplish this?

like image 215
Schildmeijer Avatar asked Jan 20 '09 13:01

Schildmeijer


People also ask

How can send mail after registration with activation link in ASP NET MVC?

Open Visual Studio and go to File->New ->Web application ->select MVC ->OK. Now, add tables in Web API project using Entity Framework. For this, go to Models folder ->right-click -> Add -> New item -> ADO.NET Entity Data Model -> click Add -> select database first approach->click Next.


1 Answers

A more up to date method would be to use System.Net.Mail - this is the 2.0 replacement for System.Web.Mail.

Something like this, called from either a BaseController (if there are other controllers that need this) the actual controller in question.

I have the following code inside a static class to handle mailing simple plain text items from the server:

internal static void SendEmail(MailAddress fromAddress, MailAddress toAddress, string subject, string body)
{
    var message = new MailMessage(fromAddress, toAddress)
                      {
                          Subject = subject,
                          Body = body
                      };

    var client = new SmtpClient("smtpServerName");
    client.Send(message);
}

Obviously, you'd probably want some error handling etc in there - Send can throw an exception for example if the server is refusing connections.

like image 85
Zhaph - Ben Duguid Avatar answered Oct 12 '22 22:10

Zhaph - Ben Duguid