How to send Email in asp.net mvc app [Resolved]

Posted by Dev080692 under ASP.NET MVC on 12/23/2015 | Points: 10 | Views : 682 | Status : [Member] | Replies : 4
how to send email in asp.net mvc app? I have to send emails through asp.net mvc app,how to send email or respond to any user through email?




Responses

Posted by: Sheonarayan on: 12/23/2015 [Administrator] HonoraryPlatinum | Points: 50

Up
1
Down

Resolved
Sending email in ASP.NET is similar to sending email in normal ASP.NET application. Below is my code snippet to declare the SendEmail method. You can copy-paste in a new class file and use it.

Send email method declaration


public class EmailSender
{
public static bool SendEmail(string toAddress, string replyToAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
{
try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
{

MailAddress fromAddress = new MailAddress("noreply@myDomain.com", "NoReply, MyDomain.Com");
// You can specify the host name or ipaddress of your server
smtpClient.Host = "smtp.MyDomain.com"; //you can also specify mail server IP address here

//Default port will be 25
smtpClient.Port = 25;

NetworkCredential info = new NetworkCredential("noreply@myDomain.com", "Password");

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = info;

//From address will be given as a MailAddress Object
message.From = fromAddress;
message.Priority = priority;
// To address collection of MailAddress
message.To.Add(toAddress);
message.Subject = subject;

if (!string.IsNullOrWhiteSpace(replyToAddress))
{
message.ReplyToList.Add(new MailAddress(replyToAddress));
}
if (!string.IsNullOrWhiteSpace(ccAddress))
{
message.CC.Add(ccAddress);
}
if (!string.IsNullOrWhiteSpace(bccAddress))
{
message.Bcc.Add(bccAddress);
}

//Specify true if it is html message
message.IsBodyHtml = isHtml;

// Message body content
message.Body = body;

// Send SMTP mail
smtpClient.Send(message);
}
}

return true;
}
catch (Exception ee)
{
throw ee;
}
}
}

You will need to ensure that you are setting correct mail server name, username and password. These things are generally provided by server folks or your IT department.


Using send email method

EmailSender.SendEmail(toEmailId, replyToEmailId, ccEmailId, bccEmailId, subject, bodyOfEmail, System.Net.Mail.MailPriority.High, true);

You can change the MailPriority based on your requirement. Declare all above variables and set its value.

If you want a complete description of how things are working, here is a great tutorials http://www.dotnetfunda.com/articles/show/1106/sending-email-with-attachment-in-aspnet.

Thanks, hope this helps.

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Dev080692, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
@Dev080692 Sir,

Sheo Sir has already replied . I would like to add a little to it. A few days back we presented another article on the same. You can refer that http://www.dotnetfunda.com/articles/show/3185/generate-pdf-and-send-as-attachment-in-email-using-itextsharp-and-exce also.

Hope that helps.

--
Thanks & Regards,
RNA Team

Dev080692, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dev080692 on: 12/24/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks

Dev080692, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/24/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Dev080692 Sir, In that case please accept the answer of your choice.

--
Thanks & Regards,
RNA Team

Dev080692, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response