0
votes

I am using Razor to send an E-mail through SMTP. I am using the same bit of code to do this in a different site, but I am getting an error on this one. I've changed the Web.config SMTP Settings. I'm not sure what I may be doing wrong.

@helper SendContactEmail(ContactFormModel formModel)
{
MailMessage mm = new MailMessage("[email protected]",          "[email protected]");
mm.Subject = "New mail from J&M Services Contact Form";
//mm.Body = string.Format("Name: {0}\nEmail: {1}\n\nMessage: {2}", formModel.Name, formModel.Email, formModel.Comment);
mm.Body = "testing body";
mm.IsBodyHtml = false;

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
    client.Send(mm);
}
catch (Exception ex)
{
    <h2>@ex.Message</h2>
    <p>@ex.StackTrace</p>
}
}

The error I'm getting is "Failure sending mail" and the StackTrace is "at System.Net.Mail.SmtpClient.Send(MailMessage message) at ASP._Page_macroScripts_Contact_cshtml.b__16(TextWriter __razor_helper_writer) in c:\Dev\SVN\Main\UmbracoApps\umbJMServices\umbJMServices\macroScripts\Contact.cshtml:line 262".

Line 262 is the client.Send(mm);

I am getting an InnerException:

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)

1
That usually means a problem with your SMTP server. Is there an InnerException?SLaks
Do you have an SMTP server defined in your web.config?Andy Evans
I added my InnerException to the question. @SLaksEric
I do have an SMTP server defined in the Web.config, and it is exactly the same as the working site's. @AndyEvansEric
You probably have a firewall in the way.SLaks

1 Answers

2
votes

I had this problem within my company because the firewall rejected the mail. Check out if there is a firewall with your system administrator.