Search
Monday, November 12, 2018 ..:: Home ::.. Register  Login

Peter Henry on Facebook  Peter Henry on LinkedIn  Peter Henry on Twitter

   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

Using the MS PasswordRecovery object and getting an SMTP Server authentication connection error, server response was 5.7.0, must issue a STARTTLS command first?

Sep 27

Written by:
Saturday, September 27, 2008 2:00 PM  RssIcon

Are you getting this error message?

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 9sm3590188qbw.6

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. 9sm3590188qbw.6

Are you using the Microsoft.NET PasswordRecover object and the LocalServer connection string in web.config?  Are you using Google's smtp servers as well?  Well, THERE'S your problem (taken from Adam Savage's quote).&l

Is this rather RUDE exception staring you in the face today/tonight?

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.

Are you using Google's Email Application as your SMTP server? Google GMail  Are you using the default behaviour for your PasswordRecovery WebControl on your web page?  If you answered YES to these three questions, then I can help you!  I found myself in this situation this weekend and although there are pieces of solutions out on the web, I will attempt to put everything into one place.

We are going to:

  1. Setup web.config for emailing
  2. Write some code 

Setup Web.Config

First, let's make sure your web.config is setup properly.  How do we do that?  Make sure you have the ASP.NET Configuration button on your Solution Explorer and click it.

ASP.NET Configuration button in Solution Explorer

After you click it, you'll see the "Welcome to the Web Site Administration Tool message.  Click on the Security tab at the very top.

ASP.NET Configuration button in Solution Explorer

If you see a message about The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty."  Then you are missing an in your web.config file and need to fix that before doing anything else.  You will need this in order to be able to have a source of information for your user accounts and passwords.  Without that, you're not sending much of anything out! :>

Missing/Invalid LocalServer ConnectionString in web.config

Ok, once you get that last problem fixed, or if you've already got your web.config setup properly, then you'll see a web page which will allow you to create/manage Uers, disable/manage Roles and create/manage Rules.  Great!  Now click on the Application tab at the top, and then where it says SMTP Settings, click the Configure SMTP e-mail settings.  Now, enter in your GMail SMTP settings.  In case you don't know what they are, here are some tips.

Configure SMTP Settings

Click on Save and OK and you're web.config settings are all setup.  You might want to go look at your web.config and check out the new/modified mailSettings/smtp/network xml nodes.  Your password's are in clear text but encryption is for another blog!  Right now, we just want to get this emailing out to work!

PS  If you don't know what these settings are, uh, hhhhhmm then you should probably look into that before continuing.  Although you can get away without knowing this stuff, it's kind of useful information to know what is going on and how it's being used.  For example, the Server Port is rather important and if you're behind a Firewall and STILL getting errors with emails going out, you'll need to see if your firewall is blocking requests going out on that port.

Setup PasswordRecovery

There's really nothing to do here with setting up the PasswordRecovery WebControl.  It's the default behaviour of the sending of the email that's the problem, so let's move onto tackling that next. 

Write Some Code

 In the Page_Load method, we're going to want to add an event handler to the PasswordRecovery.SendingMail event.

Configure SMTP Settings

If you're using Intellisense and tabbing along, you'll get the defaults and it'll look like this.

 

 

new MailMessageEventHandler(PasswordRecovery_SendingMail);

Next, you'll need to add code that looks like the following (which I'm assuming you'll be able to understand once you see it, but the highlights will follow after the code).

protected void PasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
        {
            //send email to user with their password
            String currentTime = DateTime.Now.ToString("MM-dd-yyyy hh:mm tt");
            String username = PasswordRecovery.UserName;
            MailMessage email = e.Message;
            email.Subject = Constants.ApplicationName + " Password Information";
            Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)webConfig.GetSectionGroup("system.net/mailSettings");
            NetworkCredential MailAuthentication = new NetworkCredential(mailSettings.Smtp.From, mailSettings.Smtp.Network.Password);
            SmtpClient smtpClient = new SmtpClient(mailSettings.Smtp.Network.Host, mailSettings.Smtp.Network.Port);
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = MailAuthentication;
            if (email.From == null || String.IsNullOrEmpty(email.From.Address))
            {
                email.From = new MailAddress(mailSettings.Smtp.From, Constants.ApplicationName + " Web Support Administrator");
            }
            smtpClient.Send(email);
            //have to do this otherwise you'll still see the error message from Gmail
            e.Cancel = true;
        }
 
  • using the MailMessage in the event args (contains the From, To, Subject and Body already filled out)
  • Configuration class but using the WebConfigurationManager to get the SMTP information you entered in above
  • NetworkCredentials cause you're going to have to use an encrypypted connection to GMail and this is how you do it
  • SmtpClient cause this is where the previous info gets used
  • e.Cancel = true cause otherwise you'll STILL get the error which brought to this blog in the first place

Once you do all this, you'll start to se emails going out and coming in (if you're testing :>).

Summary:  If you're using the default PasswordRecovery WebControl with Google's GMail as your SMTP server, you're going to run into problems (secure connections and authentication problems).  You'll need the code shown here to satisfy GMail's information for logging in and sending out passwords to your users.

References: In my head and coffee the next morning cause I worked on this for a while! :>

 

 

PasswordRecovery.SendingMail +=

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

2 comment(s) so far...


Re: Using the MS PasswordRecovery object and getting an SMTP Server authentication connection error, server response was 5.7.0, must issue a STARTTLS command first?

Hi,
I read your post on the DNN forums regarding wanting to format source code in DNN. It just so happens I solved this little problem myself last night, and quickly blogged about it. Thought you might be intrested in my post
Syntax Highlighting

By Gary Woodfine on   Friday, February 13, 2009 10:51 AM

Re: Using the MS PasswordRecovery object and getting an SMTP Server authentication connection error, server response was 5.7.0, must issue a STARTTLS command first?

Thank you very much Gary! I'm going to that page to read about the article now!

By phenry on   Friday, February 13, 2009 10:52 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement