Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

How to send email in Asp.net MVC application

Let’s learn how to send email in asp.net application, below we have provided all details step by step

Email
Message


Step 1: Create a form like above form, You can add all other fields like Subject, CC, BCC etc. now we have removed all those fields, so you can understand the core mail sending functionality
@using (Html.BeginForm("sendmail", FormMethod.Post))
{
<div>
<div>Email</div>
<div>
<input type="text" id="txtEmail" style="font: 18px; padding: 5px; width: 350px;" />
</div>
<div>Message</div>
<div>
<textarea id="txtContent" style="font: 18px; padding: 5px; width: 350px; height: 100px;"></textarea>
</div>
<div style="text-align: center;">
<input type="submit" value="Send Email" />
</div>
</div>
}


Step 2: Add your SMTP details in web.config file, it will be good if keep configuration details in a separate File, and read the file using configuration framework
<SmtpServer>my-SMTP.serverName.IPAddress</SmtpServer>
<SmtpUserName>smtpUserName@mydomain.com</SmtpUserName>
<SmtpPassword>mySMTPPassword</SmtpPassword>
<SMTPPort>25</SMTPPort>
<DisplayName>WebTrainingRoom.com</DisplayName>
<FromEmail>support@webtrainingroom.com</FromEmail>
<ReplyTo>webtrainingroom@gmail.com</ReplyTo>


Step 3: Now create a utility class with some name, here we have created WTRMail, inside that create a method called SendMail() with two object MailMessage and SmtpClient
public class WTRMail {
MailMessage objMail = new MailMessage(MFrom, MTo, MSubject, MBody);
//Default setting
objMail.Priority = MailPriority.Normal;
objMail.IsBodyHtml = this.IsHtmlBody;
objMail.ReplyTo = new MailAddress(this.ReplyTo);
}


Step 4: Read all your SMTP configuration information from web.config or configuration framework object
public class WTRMail
{
string _SmtpServer= System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
string _SmtpUserName= System.Configuration.ConfigurationManager.AppSettings["SmtpUserName"];
string _SmtpPassword= System.Configuration.ConfigurationManager.AppSettings["SmtpPassword"];
string _SMTPPort= System.Configuration.ConfigurationManager.AppSettings["SMTPPort"];


private bool SendETMail()
{
SmtpClient objSmtp = new SmtpClient();
objSmtp.EnableSsl = false ;
objSmtp.Host = _SmtpServer;
objSmtp.Port = _SMTPPort;
objSmtp.Credentials = new System.Net.NetworkCredential(_SmtpUserName, _SmtpPassword);
objSmtp.Send(objMail);
}
}

Comment
Name Email Website
Subscribe
 
Send Email in Asp.Net MVC


Asp.net SMTP Example

Group Training