asp.net-mvcsmtpclientservicepoint

Cannot send email using SmtpClient in ASP.NET


I want to send email in ASP.NET MVC website but it doesn't work.

I think the problem is with this exception here

'client.ServicePoint.Address' threw an exception of type 'System.NotSupportedException' with type System.Uri {System.NotSupportedException}.

after the code

var client = new SmtpClient("smtp.office365.com", 25)

In this document https://learn.microsoft.com/en-us/dotnet/api/system.net.servicepoint.address?view=netcore-3.1, it says the exception is due to "The ServicePoint is in host mode." which I have no idea.

Could anyone explain it and tell me how to resolve?

Thanks,


Solution

  • In .NET, SMTP settings has its own config section (beneath the system.net element) in the app.config and web.config file:

    <mailSettings>
        <smtp deliveryMethod="Network">
          <network host="smtp.office365.com" port="25" enableSsl="true" />
        </smtp>
    </mailSettings>
    

    You can read more about SMTP here

    And this answer in the community may help you.