Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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();
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;
catch (Exception ex)
string _errorMsg = ex.Message;
And my mailSettings code is
<smtp from="email@example.com" deliveryMethod="Network">
<network host="smtp.gmail.com" userName="firstname.lastname@example.org" password="****" port="587" enableSsl="true" defaultCredentials="false"/>
Please suggest me a good solution.
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?
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?
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.
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?
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted