You've set the
NetworkCred.Password
, but not the
NetworkCred.UserName
. Most servers will require both a username and a password for authentication.
But there's a better way: put your configuration in the
<smtp> Element (Network Settings)[
^]:
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="example@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
defaultCredentials="false"
userName="YOUR-USER-NAME"
password="YOUR-PASSWORD"
/>
</smtp>
</mailSettings>
</system.net>
</configuration>
With that in place, you don't need to configure the
SmtpClient
; the default constructor will automatically read the settings from the config file.
SmtpClient smtp = new SmtpClient();
smtp.Send(Msg);