An issue in mail sending a Contact form Email Template to an email address using MVC
Hi,
I'm using the MVC Contact form and I wrote the code for SendEmail in my Controller Action method and I get the mailSettings details from mailSettings. Now I'm unable to send the emails from the deployed cloud website but its working good in my local project.
Below is my SendEmail method.
private int SendEmail(string subject, string mailBody, string toAddress, HttpPostedFileBase file)
{
if (toAddress != null)
{
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
MailSettingsSectionGroup mailSettings = configurationFile
.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.From = new MailAddress(mailSettings.Smtp.From, "My Website");
msg.Body = mailBody.ToString();
msg.To.Add(new MailAddress(toAddress));
if (file != null)
{
string fileName = Path.GetFileName(file.FileName);
msg.Attachments.Add(new Attachment(file.InputStream, fileName));
}
using (var smtp = new SmtpClient())
{
smtp.Host = mailSettings.Smtp.Network.Host;
smtp.Port = mailSettings.Smtp.Network.Port;
smtp.UseDefaultCredentials = mailSettings.Smtp.Network.DefaultCredentials;
smtp.EnableSsl = mailSettings.Smtp.Network.EnableSsl;
var credential = new NetworkCredential
{
UserName = mailSettings.Smtp.Network.UserName, // replace with valid value
Password = mailSettings.Smtp.Network.Password // replace with valid value
};
smtp.Credentials = credential;
try
{
smtp.Send(msg);
}
catch (Exception ex)
{
string _errorMsg = ex.Message;
}
finally
{
}
}
}
}
return 1;
}
The code and the mailSettings are same in both the local and deployed sites. The form data is saved successfully to cloud database but I didn't get the email from the cloud project. And I didn't get any log exceptions in my log file. Is it requires any SSL certification? or is it need to bypass the certification validations?
How I can check whether my mail port is blocked or not? and If mail port(587) is blocked then what can I do for this?
I have a small dought in mailSettings is by default Umbraco LATCH provides free TLS (HTTPS) certificates but I used the SSL mailSettings and I entered enableSsl="true", is it correct?
An issue in mail sending a Contact form Email Template to an email address using MVC
Hi, I'm using the MVC Contact form and I wrote the code for SendEmail in my Controller Action method and I get the mailSettings details from mailSettings. Now I'm unable to send the emails from the deployed cloud website but its working good in my local project. Below is my SendEmail method.
And my mailSettings code is
Please suggest me a good solution.
Thanks
Ram's
Hi Ram's,
Have you actually compared the files between your local site and the deployed site?
Are you sure that the mail settings that you are using locally have actually been pushed up to the deployed site?
Are you getting any exceptions in the Log file Or YSOD when running the site?
Thanks
Craig
Thank you, Craig Mayers, for your great response.
The code and the mailSettings are same in both the local and deployed sites. The form data is saved successfully to cloud database but I didn't get the email from the cloud project. And I didn't get any log exceptions in my log file. Is it requires any SSL certification? or is it need to bypass the certification validations?
Thanks
Ram's
Hi Ram's,
Hmmm, OK.
So it doesn't sound like an exception is occurring in code then... Maybe you mail port (587) is being blocked.
Check your server firewall to ensure that you have an exception set for the port you are using for SMTP.
Thanks
Craig
Thank you, Craig.
How I can check whether my mail port is blocked or not? and If mail port(587) is blocked then what can I do for this?
I have a small dought in mailSettings is by default Umbraco LATCH provides free TLS (HTTPS) certificates but I used the SSL mailSettings and I entered enableSsl="true", is it correct?
Thanks
Ram's
is working on a reply...