Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ram's Reddy 10 posts 80 karma points
    4 days ago
    Ram's Reddy
    0

    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;
        }
    

    And my mailSettings code is

    <mailSettings>
      <smtp from="mymail@gmail.com" deliveryMethod="Network">
        <network host="smtp.gmail.com" userName="mymail@gmail.com" password="****" port="587" enableSsl="true" defaultCredentials="false"/>
      </smtp>
    </mailSettings>
    

    Please suggest me a good solution.

    Thanks

    Ram's

  • Craig Mayers 164 posts 485 karma points
    4 days ago
    Craig Mayers
    1

    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

  • Ram's Reddy 10 posts 80 karma points
    4 days ago
    Ram's Reddy
    0

    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

  • Craig Mayers 164 posts 485 karma points
    4 days ago
    Craig Mayers
    0

    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

  • Ram's Reddy 10 posts 80 karma points
    3 days ago
    Ram's Reddy
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft