Send Email (SMTP) RRS feed

  • Question

  • User-322454893 posted

    I'm using ASP.Net (MVC) to send an email from a website.

    When I have tried to send it in debug mode, I have the following error:

    System.Net.Mail.SmtpException: Server does not support secure connections. at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result) at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at BL.Controllers.HomeController.<TryMe>d__5.MoveNext() in D:\Main\01. BL\01. Projects\03. Website\02. Code\BL\BL\BL\Controllers\HomeController.cs:line 186

    After publishing it, I have the following error:

    The remote certificate is invalid according to the validation procedure.

    I have asked the host provider (Hostgator) to help me and he told me that the error occurred due to wrong configuration in my script, but he can't detect what is it exactly as his development skills is limited.

    This is the Configuration that I have used in web.config file:

    <smtp from="mail@b-l.group">
    <network host="hgws28.win.hostgator.com" enableSsl="true" port="25"
    userName="***" password="***"/>

    And this is the Code that I have used in my Controller file:

    public async Task<ActionResult> TryMe(EmailFormModel model)
    bool validData = IsValidData(model);

    if (model != null && validData)
    var receiverEmail = new MailAddress("project@b-l.group");
    var subject = "Online Service";
    var body = GetMailBody(model);

    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
    return true;

    MailMessage message = new MailMessage();
    message.Subject = subject;
    message.Body = body;

    using (SmtpClient smtp = new SmtpClient())
    await smtp.SendMailAsync(message);
    ViewBag.Verified = "Data Sent!";

    return View(model);
    ViewBag.Warning = "Fill missed data!";
    catch (Exception ex)
    ViewBag.Warning = ex.ToString();

    return View(model);

    What is the problem in my script?

    Wednesday, April 17, 2019 9:37 AM

All replies

  • User-943250815 posted

    <network host="hgws28.win.hostgator.com" enableSsl="true" port="25"

    System.Net.Mail.SmtpException: Server does not support secure connections.

    You mixing SSL with Port 25, for SSL usually port is 587, so you have 2 choices
    a) Change to port 587, or check provider to use proper port
    b) Change enableSsl to false and leave port 25

    Wednesday, April 17, 2019 12:42 PM
  • User-322454893 posted

    a) For the first solution, I have asked my provider (Hostgator) and he said that it is port 25 not 587

    b) For the second solution, I have changed enableSsl to false and the same error exist.

    Wednesday, April 17, 2019 1:12 PM
  • User-943250815 posted

    It´s been a long time I used SmtpClass with/without web.config settings.
    But I think found why you getting System.Net.Mail.SmtpException: Server does not support secure connections.
    Here you find more about https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/network-element-network-settings

    On remarks you find
    "The System.Net.Mail.SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207"

    I suggest you try Mailkit, as proposed in in System.Net.Mail.Smtpclient above

    Wednesday, April 17, 2019 9:19 PM