Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP Authentication with config file's MailSettings

I'm storing my MailSettings in a web.config, however when I send the message, my SMTP server reports back that I need to use authentication. I've got my username/password in the config file, but it still fails.

It works if I do the following, but it seems like an extra step. Shouldn't it just take it from the config file and use authentication automatically?

System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(
    HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings =
    (MailSettingsSectionGroup) config.GetSectionGroup("system.net/mailSettings");

SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential(
   settings.Smtp.Network.UserName, settings.Smtp.Network.Password);

Web.config

<system.net>
    <mailSettings>
        <smtp from="[email protected]" deliveryMethod="Network">
            <network host="mail.xyz.com" defaultCredentials="true"
                userName="me@xyzcom" password="abc123" />
        </smtp>
    </mailSettings>
 </system.net>

System.Net.Mail.SmtpException

Exceeded storage allocation. The server response was: Please use smtp authentication. See http://www.myISP.com/support/smtp-authentication.aspx

The "Exceeded storage allocation" confused us for quite awhile, we now ignore it. It's the "use smtp authentication" that seems to be important.

like image 904
Eric Avatar asked Sep 01 '10 18:09

Eric


People also ask

What is SMTP authentication method?

SMTP Authentication is the mechanism by which the clients of an ISP identify themselves to the mail server through which they intend to send email. It is not possible for any person to send email via any mail server they choose; mail servers will only allow the sending of email by legitimate users.

How do I set SMTP server username and password?

So how to configure an SMTP authentication? The procedure is simple. You need to open your mail client, go to the SMTP configuration panel, and flag the option “Authentication Required”. Then choose the type you prefer, set a username and password, and switch your server port to 587 (recommended).

What is SMTP AUTH username?

SMTP Authentication. Specify SMTP authentication. When mail is sent to the SMTP server, authentication is performed using the SMTP AUTH protocol by prompting the mail originator to enter the user name and password. This prevents illegal use of the SMTP server.


1 Answers

The difference between the coded approach and the web.config only approach is that the latter has defaultCredentials="true" set. That is preventing the username and password from being used to authenticate, with that approach. I think the problem would be solved by setting that to “false” (or removing it completely, because “false” is the default).

like image 89
Mike Green Avatar answered Oct 22 '22 07:10

Mike Green