Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 November 25th, 2009, 09:12 PM
Registered User
 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing e.Message of Sending mail event

I am currently on page 533 chapter 15 and am strugling to reconfigure the sending mail event to allow the wizad tool to send an email with the username and password enabling SSL.

Please could you help me with the necessary code to enable this in C# as i can only do this by removing the wizad and creating my own form however this defeats the object of having a wizard.

I have copy the code from the book as suggested in chapter 9 but am unable to pass the values through.

Please could you assist me with the necessary code to pass the values through

Thank you in advance.
Hiren
 
Old November 26th, 2009, 04:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at these posts:

Chap 15 pg 532
Chapter 15: SendingMail Event

They explain how to handle this. If that doesn' help (enough), can you post your current code and describe the exact problems you're experiencing?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old November 26th, 2009, 07:39 PM
Registered User
 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default sending email with createuser wizard

Dear Imaar,
Thank you for your quick response to my questions yesterday. However i have struggled to enable the ssl in order to pass the email message through to the new user.Please could you look at my code below and assist me.



Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net.Mail;
using System.Web.UI.WebControls;

public partial class SignUp : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
    {
        
        e.Message.IsBodyHtml = false;
        e.Message.Subject = "New User on website";
        e.Message.Body.Replace("<%UserName%>",CreateUserWizard1.UserName);
        e.Message.Body.Replace("<%Password%>",CreateUserWizard1.Password);


        

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Port = 465;
        mySmtpClient.Credentials = new
        System.Net.NetworkCredential("[email protected]", "mypassword");
            mySmtpClient.EnableSsl = true;
            mySmtpClient.Send(e.Message);

            
        }
    }
Below is the mark up code

Code:
<%@ Page Title="Sign Up" Language="C#" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="true" CodeFile="SignUp.aspx.cs" Inherits="SignUp" %>

<script runat="server">

    
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:CreateUserWizard ID="CreateUserWizard1" runat="server">

               <MailDefinition BodyFileName="~/App_Data/SignUpConfirmation.txt" 
            Subject="Your new account at PlanetWrox.com">
        </MailDefinition>

        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" />
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
        </WizardSteps>
    </asp:CreateUserWizard>
</asp:Content>
I have managed to send an email message in chapter 9, however now i am constantly presented with a starttls error.

Please could you advice me how i need to change the code as i cannot move on with my learning in your great book.

Hiren
 
Old November 28th, 2009, 08:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

There are a couple of things wrong with your code:

1. You're not using the right port number
2. You're not cancelling the original message
3. You don't need to use the Replace method to change the message body as that has already been done for you. The following should work:
Code:
 
protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
{
  SmtpClient mySmtpClient = new SmtpClient();
  mySmtpClient.EnableSsl = true;
  mySmtpClient.Send(e.Message);
  e.Cancel = true;
}
This code creates a new SmtpClient, enables SSL, sends the message using the new client and then cancels the original message to stop the Wizard from sending its own message. For this code to work, you need the following config section:
Code:
 
<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network 
          port="587" 
          userName="[email protected]" 
          password="YourPassword" 
          host="smtp.gmail.com"
        />
    </smtp>
  </mailSettings>
</system.net>
Don't forget to add your Gmail account name and password in the config file.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old November 28th, 2009, 08:16 PM
Registered User
 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default sending email with createuser wizard

Dear Imaar,

Thank you very much for you quick prompt reply. I have actioned the changes as per your reply however I am now getting a new error!

Server Error in '/' Application.
A from e-mail address must be specified in the From property or the system.net/mailSettings/smtp config section.

Code:
<%@ Page Title="Sign Up" Language="C#" MasterPageFile="~/Master Pages/MasterPage.master" AutoEventWireup="true" CodeFile="SignUp.aspx.cs" Inherits="SignUp" %>

<script runat="server">

    
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
   <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" MailDefinition-IsBodyHtml="False" MailDefinition-BodyFileName="App_Data/SignUpConfirmation.txt" MailDefinition-Subject="your new account">
		<MailDefinition BodyFileName="~/App_Data/SignUpConfirmation.txt" 
            Subject="Your New Account at PlanetWrox.com">
		</MailDefinition>
		<WizardSteps>
			<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" />
			<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
		</WizardSteps>
	</asp:CreateUserWizard>
</asp:Content>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Net.Mail;
using System.Web.UI.WebControls;

public partial class SignUp : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }



    protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
    {
    

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.EnableSsl = true;
        mySmtpClient.Send(e.Message);
        e.Cancel = true;
    }

}
Code:
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network
          port="587"
          userName="myusername"
          password="mypassword"
          host="smtp.gmail.com"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
I am unable to reslove this problem and not sure about e.message, can you recommend anywhere to read about that is on the net.

Many Thanks.

Hiren
 
Old November 29th, 2009, 04:04 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

If it really just needs a from address, here's how I declare mine in my contact form...

e.From = new MailAddress("Admin <[email protected]>");
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old November 29th, 2009, 06:14 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

chroniclemaster1's solution should indeed work fine.

If you want to fix the issue for all e-mails sent by your site, you can also add a from attribute to the smtp element in web.config:

<smtp deliveryMethod="Network" from="[email protected]">

To make it easier to answer this question in the future, I wrote a quick article about it, with the full source of a demo application included: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=505

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old November 30th, 2009, 04:17 PM
Registered User
 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Imaar,

Thank you for your recent response, after trying your method and reading the applying the other information you gave me i am still getting a error as detailed below.

Server Error in '/' Application.
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. 7sm9177eyg.33
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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. 7sm9177eyg.33

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Code:
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network
          port="587"
          userName="username"
          password="password"
          host="smtp.gmail.com"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
Code:
 protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e)
    {
        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.EnableSsl = true;
        mySmtpClient.Send(e.Message);   
        e.Cancel = true;

    }
The only other possiblities i can think of is that gmail will not work on Asp.net development server, however it worked with the contact us form in chapter 9.


Is it a case that gmail requires both a to and from explaination in order for gmail not to class the message as possible spam.

I have also tried chronicalmaster's suggestion and found that e.from does not work it throws an error when compiling.

Have you actually run the code and has it worked for you?

Thank you for all your help up to now.

Hiren
 
Old November 30th, 2009, 04:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, I ran it and it has worked for me, or I wouldn't be writing about it.

Are you sure you are using the right user name and password? E.g. not just "you", but [email protected]?

Also, take a look here: http://mail.google.com/support/bin/a...n&answer=13287 Have you tried another port number?

chroniclemaster1's code contains a bug. It's not e.From, but e.Message.From as From is a property on the Message object.

Finally, are you sure the config file is OK and read correctly?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
problems sending mail with java mail gandacuboy J2EE 2 December 20th, 2006 03:05 PM
sending mail ashokparchuri General .NET 3 May 5th, 2005 02:42 AM
sending mail anita VB How-To 3 September 30th, 2004 03:48 PM
Sending e-mail to different mail box! Calibus Classic ASP Databases 4 September 3rd, 2004 05:48 PM
Sending both text mail and HTML mail - CDONTS madhukp Classic ASP Basics 1 October 8th, 2003 01:05 AM





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