M
Muhammad Nauman 61673
Guest
send email working fine on localhost but on live website which is running on azure giving smtp error.
// Email.cs
public string SendMail(string recieverEmail, string subject, string body)
{
try
{
string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"];
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"];
string smtpPort = System.Configuration.ConfigurationManager.AppSettings["SMTPPort"];
string host = System.Configuration.ConfigurationManager.AppSettings["Host"];
var sender = new MailAddress(senderEmail, "Petersham");
var receiver = new MailAddress(recieverEmail, "Receiver");
var password = senderPassword;
var smtp = new SmtpClient
{
Host = host,
Port = Convert.ToInt16(smtpPort),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(sender.Address, password)
};
using (var mail = new MailMessage(senderEmail, recieverEmail)
{
Subject = subject,
Body = body,
IsBodyHtml = true,
})
{
smtp.Send(mail);
}
return "sent";
}
catch (Exception e)
{
return e.ToString();
}
}
public string MessageBody(string title, string paragraph, string footer)
{
string body = string.Empty;
using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Views/Shared/EmailTemplate.html")))
{
body = sr.ReadToEnd();
}
//parameters --- set link
body = body.Replace("{#TITLE}", title);
body = body.Replace("{#FooterText}", footer);
body = body.Replace("{#Paragraph}", paragraph);
return body;
}
// Call
Email obj = new Email();
string paragraph = @"my Paraghraph";
var emailMsg = obj.SendMail(model.Email, "Go2Eat", obj.MessageBody("Go2Eat Demo Link", paragraph, "Go2Eat"));
if (emailMsg == "sent")
{
ViewBag.Message = "Please check your email...";
}
else
{
ViewBag.Message = "Error: " + emailMsg;
}
// Web.config
<add key="SenderEmail" value="myemail" />
<add key="SenderPassword" value="mypassword" />
<add key="SMTPPort" value="587" />
<add key="Host" value="smtp.gmail.com" />
This code is working perfetly fine on local host... what should i do to solve this error and send mail??
Continue reading...
// Email.cs
public string SendMail(string recieverEmail, string subject, string body)
{
try
{
string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"];
string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"];
string smtpPort = System.Configuration.ConfigurationManager.AppSettings["SMTPPort"];
string host = System.Configuration.ConfigurationManager.AppSettings["Host"];
var sender = new MailAddress(senderEmail, "Petersham");
var receiver = new MailAddress(recieverEmail, "Receiver");
var password = senderPassword;
var smtp = new SmtpClient
{
Host = host,
Port = Convert.ToInt16(smtpPort),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(sender.Address, password)
};
using (var mail = new MailMessage(senderEmail, recieverEmail)
{
Subject = subject,
Body = body,
IsBodyHtml = true,
})
{
smtp.Send(mail);
}
return "sent";
}
catch (Exception e)
{
return e.ToString();
}
}
public string MessageBody(string title, string paragraph, string footer)
{
string body = string.Empty;
using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Views/Shared/EmailTemplate.html")))
{
body = sr.ReadToEnd();
}
//parameters --- set link
body = body.Replace("{#TITLE}", title);
body = body.Replace("{#FooterText}", footer);
body = body.Replace("{#Paragraph}", paragraph);
return body;
}
// Call
Email obj = new Email();
string paragraph = @"my Paraghraph";
var emailMsg = obj.SendMail(model.Email, "Go2Eat", obj.MessageBody("Go2Eat Demo Link", paragraph, "Go2Eat"));
if (emailMsg == "sent")
{
ViewBag.Message = "Please check your email...";
}
else
{
ViewBag.Message = "Error: " + emailMsg;
}
// Web.config
<add key="SenderEmail" value="myemail" />
<add key="SenderPassword" value="mypassword" />
<add key="SMTPPort" value="587" />
<add key="Host" value="smtp.gmail.com" />
This code is working perfetly fine on local host... what should i do to solve this error and send mail??
Continue reading...