Web Connection
wwSmtp with no credentials
Gravatar is a globally recognized avatar based on your email address. wwSmtp with no credentials
  Johnny
  All
  Jun 28, 2021 @ 06:30am

Hi,

Is it possible to use wwSmtp with no credentials - i.e. set UseDefaultCredentials on SmtpClient?

I have gone through the sample code and documentation but it seems in all instances, the username and password were referenced.

Thanks.

Gravatar is a globally recognized avatar based on your email address. re: wwSmtp with no credentials
  Rick Strahl
  Johnny
  Jun 28, 2021 @ 09:15am

You can't use default credentials with SMTP. There rarely is a co-relation between user accounts and system passwords anyway (nor should there be - huge security issue there unless it's all internal), unless you're using some service based tool like Exchange in which case security goes through Outlook (or other Exchange specific tool).

I think MAPI may still work for Exchange based clients but that's not something that Client Tools supports any longer (and I don't know even if that still is a thing). If you're using Outlook specifically the Outlook automation (or .NET) APIs let create and send messages using the system configured account information.

Ultimately how security is handled depends on the server and is not controlled by the client. Long, long ago you could send emails without any log ons, before people figured out there's such a thing as SPAM, LOL.

As you might have surmised - it's getting harder and harder to provide email access as part of your applications unless you use fixed accounts and a mail service of some sort. Most of that is due to security and the proliferation of malfeasance using email as the source.

If you need to send emails for users I think the best way to do this these days is to create the email and open it in the default mail client (using the mailto: moniker). That works for basic messages, but not for complex content or attachments unfortunately.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: wwSmtp with no credentials
  Johnny
  Rick Strahl
  Jun 29, 2021 @ 05:49am

Thanks as always for your reply, Rick.

My setup is a bit different. I have an SMTP Server which uses windows authentication to send emails. In such scenarios, there is no need for NetworkCredentials and SmtpClient.UseDefaultCredentials = true works. Also, I have an SSL certificate for the domain on the server, so the emails go through alright and are not flagged as SPAM.

I went through your wwSmtp code and loved how you implemented it. Would have worked for my purposes except for the username/password (NetworkCredentials) requirements. It's okay though. I have implemented it in C# (below) and use .NET bridge to load it and send emails.

private SmtpClient Client
        {
            get
            {
                if (_client == null)
                {
                    if (string.IsNullOrWhiteSpace(this.MailHost))
                    {
                        throw new Exception("Mail host not set");
                    }

                    var port = this.MailPort <= 0 ? 25 : this.MailPort;
                    var ssl = this.IsSecure;
                    var useDefault = this.IsTrustedConnection;

                    _client = new SmtpClient
                    {
                        Host = this.MailHost,
                        Port = port,
                        EnableSsl = ssl,
                    };

                    if (useDefault == true)
                    {
                        _client.UseDefaultCredentials = true;
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(this.MailAccountUsername) || string.IsNullOrWhiteSpace(this.MailAccountPassword))
                        {
                            throw new Exception("Mail user name and/or password not set");
                        }

                        _client.Credentials = new NetworkCredential(this.MailAccountUsername, this.MailAccountPassword);
                    }
                }

                return _client;
            }
        }
Gravatar is a globally recognized avatar based on your email address. re: wwSmtp with no credentials
  Rick Strahl
  Johnny
  Jun 29, 2021 @ 08:53am

Hmmm... yeah I wonder if we could make wwSMTP doe this with a special username value like LOCALACCOUNT or something. It needs a special trigger because an empty string for the username means no authentication.

Then again - this is such a rare thing these days it might not be worth it.

ps.
In the back of my mind I have it that I did this at some point with the SMTP client, but can't remember if that's in Web Connection or for some of my .NET Tools. I'll check.

psps.

Ok so added this to the SmtpClient access code:

if (!string.IsNullOrEmpty(Username))
{
   if (Username.ToUpper() == "LOCALACCOUNT")
      Smtp.UseDefaultCredentials = true;
   else
     Smtp.Credentials = new NetworkCredential(Username, Password);
}

Updated here in the Preview files zip:

Web Connection Experimental

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: wwSmtp with no credentials
  Johnny
  Rick Strahl
  Jun 29, 2021 @ 09:36am

Thanks Rick. This should work for my scenario.

© 1996-2021