3

I've used a tutorial to set up email in my MVC5 app contact form and send via email. I'm at a loss. I've tried several different options but I'm just missing something. Help much appreciated!

My mail host is rackspace and their settings are available here http://www.rackspace.com/apps/support/portal/1088.

It all works fine except i keep getting this error of the smpt.Send(MailInfo) in the SendFinalMail function.

SmtpException occurred
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The operation has timed out.

ADDED StackTrace as per comment (let me know if you need more):

System.Net.Mail.SmtpException occurred
  _HResult=-2146233088
  _message=The operation has timed out.
  HResult=-2146233088
  IsTransient=false
  Message=The operation has timed out.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
  InnerException: 

Here is my code:

Web.config

  <appSettings>

    <!--EMAIL SERVICES-->
    <add key="FromAddress" value="myfrom@address" />
    <add key="UserID" value="myfrom@address" />
    <add key="Password" value="myPassword" />
    <add key="SMTPPort" value="465" />
    <add key="SmtpClient" value="secure.emailsrvr.com" />
    <add key="EnableSSL" value="Yes" />
    <add key="UnobtrusiveJavaScriptEnabled" value ="true"/>
  </appSettings>

Models

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

namespace SandBox.MailManager
{
    class MailManager
    {
        #region Private for email
        private string FromAddress { get; set; }
        private string EmailHost { get; set; }
        private string Pwd { get; set; }
        private string UserID { get; set; }
        private string SMTPPort { get; set; }
        private Boolean bEnableSSL { get; set; }


        #endregion


#region Default Functions for Mailing
            public MailManager()
            {
                FromAddress = System.Configuration.ConfigurationManager.AppSettings["FromAddress"];
                UserID = System.Configuration.ConfigurationManager.AppSettings.Get("UserID");
                SMTPPort = System.Configuration.ConfigurationManager.AppSettings.Get("SMTPPort");
                Pwd = System.Configuration.ConfigurationManager.AppSettings.Get("Password");
                EmailHost = System.Configuration.ConfigurationManager.AppSettings.Get("SmtpClient");

                if (System.Configuration.ConfigurationManager.AppSettings.Get("EnableSSL").ToUpper() == "YES")
                {
                    bEnableSSL = true;
                }
                else
                {
                    bEnableSSL = false;
                }
            }


private bool SendFinalMail(MailMessage MailInfo)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = EmailHost;
                smtp.Port = Convert.ToInt16(SMTPPort);
                smtp.Credentials = new System.Net.NetworkCredential(UserID, Pwd);
                smtp.EnableSsl = bEnableSSL;
                //smtp.Send(MailInfo);

                System.Threading.Thread emailThread;
                emailThread = new System.Threading.Thread(delegate()
                {
                    smtp.Send(MailInfo);
                });

                emailThread.IsBackground = true;
                emailThread.Start();

                return true;
            }
            catch (Exception e)
    {
        Console.WriteLine(e);
        return false;
        }
        }
        #endregion


 public bool SendEnquiryEmail(string ToUserEmail, string htmlBody)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(FromAddress);
                mail.To.Add(ToUserEmail);
                mail.CC.Add("another@email");
                mail.Subject = "Thank you. Our team will be in touch shortly";
                mail.Body = htmlBody;
                mail.IsBodyHtml = true;

                return this.SendFinalMail(mail);
            }
            catch
            {
                return false;
            }
        }
    }
}

Controller

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Contact(ContactModel Contact)
        {
            if (ModelState.IsValid) { 
                try
                {
                    MailManager.MailManager oMail = new MailManager.MailManager();

                    string htmlBody = "<html><body>Hi, ";
                    htmlBody += "<p>We've receive your message and we will in touch shortly to responded to your message,</p>";
                    htmlBody += "<br /><br /><p>You can email us directly at <a href='#'>.</p>";
                    htmlBody += "<br /><br /><p>The message we have received from you is:</p>";
                    htmlBody += "<br /><br /><p>" + Contact.Message + "</p>";
                    htmlBody += "<p><br /> Regards,</p> </body></html>";

                    bool SendEmail = oMail.SendEnquiryEmail(Contact.Email, htmlBody);

                    if (SendEmail == true)
                    {
                        return View();
                    }
                    else { 
                        return View();
                    }
                }
                catch (Exception)
                {
                    return View();
                }
            }
            return View("Model not Valid");
        }
durbo
  • 556
  • 4
  • 11
  • You should post a full stacktrace of the error you are getting. – Rosdi Kasim Mar 08 '15 at 15:05
  • Thanks Rosdi, I've Stacktrace information please let me know if i'm missing other important details – durbo Mar 08 '15 at 15:41
  • That does not look like a complete stacktrace. You should print the stacktrace inside of your `SendEnquiryEmail` and `SendFinalMail` catch block. Right now you are returning `false` and swallowing the exception. – Rosdi Kasim Mar 08 '15 at 17:04
  • i'm fairly new to C# and mvc so i'm not sure how to do a full stack trace. I've added the catch exception. However, I can't find the Console.WriteLine data. I've taken screenshots of the exception object i can message them. Your help is much appreciated. – durbo Mar 09 '15 at 10:50
  • A timeout would suggest that the SMTP host is not available from your Rackspace server. Be that because of firewall issues or connectivity problem. – Jack Hughes Mar 09 '15 at 11:00
  • @durbo, check this out see if it helps: http://stackoverflow.com/a/26784461/193634 – Rosdi Kasim Mar 09 '15 at 12:57
  • Or you may want to try this: http://aboutcode.net/postal/ – Rosdi Kasim Mar 09 '15 at 12:59
  • I've learned the answer. The answer is: Because System.Net.Mail does not support "implicit" SSL, only "explicit" SSL. – durbo Mar 11 '15 at 04:46

1 Answers1

8

I've learned the answer. The answer is: Because System.Net.Mail does not support "implicit" SSL, only "explicit" SSL.

I'm using SSL through port 465 its causing those issues. I change the Host, Port and enableSSL to make it unsecured connection and it worked fine.

I've since found that rather than sending from the ssl port 465 if i use the TLS port 587 and enableSSL it works. This post had the answer for me

Sending email in .NET through Gmail

Community
  • 1
  • 1
durbo
  • 556
  • 4
  • 11