Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 27th, 2008, 12:59 AM
Registered User
 
Join Date: Jun 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default SMTP error in Chapter 4 - mailDefinition

Hi,
I'm trying out the Solution in this book chapter by chapter in my home computer and I'm stuck in Chapter 4 where we send emails using <mailDefinition> I was able to send email successfully from the contact.aspx page using the following web.config
<system.net>
        <mailSettings>
            <smtp deliveryMethod ="Network" from ="xxx@gmail.com">
                <network defaultCredentials="false" host="smtp.gmail.com" userName="xxx@gmail.com" password="xxx" port="587"/>
            </smtp>
        </mailSettings>
    </system.net>

and the following code

  SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Send(msg);

Please note I had to add
             client.EnableSsl = true;


But the Register.aspx and PasswordRecovery.aspx page gives the following error :(

"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. i12sm10525493wxd.4 "

Kindly help me resolve this error.
 
Old August 27th, 2008, 09:47 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You will have to interrupt the sending of the mail in the SendingMail event, and use a custom Smtp client in the event handler.

In the page, you'd have something like this:

<asp:PasswordRecovery ID="PasswordRecovery1" runat="server" OnSendingMail="PasswordRecovery1_SendingMail">
   <MailDefinition BodyFileName="passwordrecovery.txt" Subject="Your Password Reminder" />
</asp:PasswordRecovery>

Then, in code, something like this:

 protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
 {
    MailMessage message = e.Message;

    try
    {
       SmtpClient smtp = new SmtpClient();
       smtp.EnableSsl = true;
       smtp.Send(message);
    }
    catch (Exception ex)
    {
       throw new Exception("There was a problem sending the email.", ex);
    }

    e.Cancel = true;
 }
 
Old August 28th, 2008, 12:27 PM
Registered User
 
Join Date: Jun 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Lee,
It worked like a charm.

If you don't mind could you please tell me why we had to add e.cancel = true;
When I didn't have that before the program sent 2 emails instead of one.
 
Old August 28th, 2008, 12:41 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

This event handler is invoked when the PasswordRecovery control is attempting to send an email. What you are doing here is grabbing the message from that initial attempt and sending it with your own SSL-enabled SmtpClient. Then, you need to call e.Cancel to stop the original send; otherwise, it will still try to go through. This will result in two emails being sent -- one through your SSL-enabled client, and one through the original non-SSL client.

 
Old August 28th, 2008, 01:24 PM
Registered User
 
Join Date: Jun 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Somewhat makes sense Lee,
So when we didn't interrupt the natural email process, It gave "client was not authenticated", When we interrupted and created a new SmtpClient() and client.EnableSsl = true; the original email process also works (Unless we cancel it explicitly like we did)??

I'm always weak in Email process, may be I don't understand the basics.

Anyway,
Thank you so much for your time and effort!
Seetha.
 
Old August 28th, 2008, 02:25 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
quote:Originally posted by seetha_l


So when we didn't interrupt the natural email process, It gave "client was not authenticated", When we interrupted and created a new SmtpClient() and client.EnableSsl = true; the original email process also works (Unless we cancel it explicitly like we did)??
Yes, that's exactly right.






Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP Email Error Handling Ron Howerton Pro Visual Basic 2005 1 July 12th, 2007 08:32 AM
how to send the username with Maildefinition? hertendreef ASP.NET 2.0 Professional 2 May 24th, 2007 01:55 AM
problem with Maildefinition hertendreef ASP.NET 2.0 Professional 2 May 5th, 2007 12:54 PM
Error in mail() from IIS/SMTP surendran PHP How-To 2 January 22nd, 2005 08:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.