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 December 30th, 2009, 05:23 PM
Authorized User
 
Join Date: Dec 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 9:page 310,sending email messages

Hi,

I am working on try it out exercise on page 309. Here is my code from Email.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Email : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
      MailMessage myMessage = new MailMessage();
      myMessage.Subject = "Test message";
      myMessage.Body = "hello world,from planet wrox";
      myMessage.From = new MailAddress("myemailid", "my name");
      myMessage.To.Add(new MailAddress("receiver's emailid", "receiver's name"));
     
      SmtpClient mySmtpClient = new SmtpClient();
      mySmtpClient.Send(myMessage);
      mySmtpClient.EnableSsl = true;

    }
}
and here is the code, i added to web.config file

Code:
 <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="my emailid">
        <network host="smtp.myprovider.com"/>

      </smtp>
    </mailSettings>
  </system.net> 
  
</configuration>
and when i run my page in to browser i get the following error

A socket operation was attempted to an unreachable network xx.xxx.xx.xxx:xx
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.Sockets.SocketException: A socket operation was attempted to an unreachable network xx.xxx.xx.xxx:xx

Source Error:


Line 19:
Line 20: SmtpClient mySmtpClient = new SmtpClient();
Line 21: mySmtpClient.Send(myMessage);
Line 22: mySmtpClient.EnableSsl = true;
Line 23:


Source File: c:\BegASPNET\Site\Demos\Email.aspx.cs Line: 21

I am stuck here, can anyone help me to figure it out.

thanx in advance.

Arya
 
Old December 30th, 2009, 09:29 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

I think your problem is that you're specifying information in the web.config, but nothing in your code actually calls to it. There are two different ways you could go about this. I do have a completely different piece of code that accesses information in the the web.config. I'm not sure how to adapt it to your situation, but it fetches the connection string for a database from the web.config and passes it to the C# code, http://beta.earthchronicle.com/ECBet...sLayerDal.aspx.

More useful to you, might be the code for the user control I used to create my own email contact form. It's very simple with just a textarea for comments, an optional email address textbox, and a submit button. Here's the code for the user control.

<EmailContactform.ascx>
Code:
<div id="contactForm" class="contactForm" tabindex="0">

    <div id="contactImage" title="Contact Us!"></div>

    <h2 title="Contact the Best Public Domain Library on Earth">Have a Question? Leave a comment!</h2>

    <h4 title="Make the Best Public Domain Library on Earth better!">Add an update, make a suggestion, or report an error.</h4>

    <fieldset>
    <!-- NOTE: Label tags use the for attribute to connect with the id of a particular control. -->

        <legend>Contact Us!</legend>

        <ol>
            <li>
                <label for="ctl00_ctl00_placeholderForContactForm_component_ContactForm_questionsAndCommentsTextarea">Questions and Comments</label>
                <asp:textbox runat="server" id="questionsAndCommentsTextarea" textmode="multiLine" />
            </li>
            <li>
                <label for="ctl00_ctl00_placeholderForContactForm_component_ContactForm_emailTextbox">Email (optional)</label>
                <asp:textbox runat="server" id="emailTextbox" />
            </li>
            <li>
                <asp:button runat="server" id="button_EmailQuestionsAndComments" text="Email Us!" OnClick="EmailQuestionsAndComments" />
                <asp:Label runat="server" ID="contactFormMessage"></asp:Label>
            </li>
        </ol>

    </fieldset>

</div>
and here's the C# code that makes it work
<EmailContactform.ascx.cs>
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ContactForm : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //testing to see if the page works
        this.contactFormMessage.Text = "Ask Us!";
    }
    
    protected void EmailQuestionsAndComments(object sender, EventArgs e)
    {
        // Declaring necessary mail variables
        MailMessage contactFormEmail = new MailMessage(); // the email (technically mail message)
        NetworkCredential emailAccountCredentials = new NetworkCredential("systemEmailUserName", "systemEmailPassword"); // the authentication credentials
        SmtpClient smtpAccount = new SmtpClient("mail.myDomain.com", 25); // the SMTP account and port#
        Uri urlOfVisitedPage = Request.Url; // the URL of the current webpage (this subroutine will run on every page of the website, so we don't know if we don't go grab it)

        // the email configuration information: sender, recipients, subject
        contactFormEmail.From = new MailAddress("Admin <[email protected]>");
        contactFormEmail.To.Add(new MailAddress("[email protected]")); // Copy this line to send to multiple email addresses
        // contactFormEmail.To.Add(new MailAddress("[email protected]"));
        contactFormEmail.Subject = "I have a question or comment.";
        contactFormEmail.IsBodyHtml = true;

        // the body of the email
        contactFormEmail.Body = "<p>Hi!</p> <p>You have a message from an Earth Chronicle visitor. They responded to <a href='" + urlOfVisitedPage.AbsoluteUri + "'>" + urlOfVisitedPage.AbsoluteUri + "</a></p>";
        contactFormEmail.Body += "<p>Their email is: " + this.emailTextbox.Text + "</p>";
        contactFormEmail.Body += "<p>They write:" + this.questionsAndCommentsTextarea.Text + "</p>";
        contactFormEmail.Body += "<p>TTFN,<br />the Server</p>";

        // SMTP configuration information and use the Send method
        smtpAccount.UseDefaultCredentials = false;
        smtpAccount.Credentials = emailAccountCredentials;
        smtpAccount.Send(contactFormEmail);

        // notify the visitor that the procedure has successfully completed
        this.contactFormMessage.Text = "Sent.";
    }
}
Does that work?
__________________
-------------------------

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 January 2nd, 2010, 01:57 PM
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 this:

SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
mySmtpClient.EnableSsl = true;

You're specifying SSL *after* you've sent the message. Also, you're not specifying a port number anyware (not in code, not in the config). Which mail server are you trying to connect to?

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 January 4th, 2010, 09:43 PM
Authorized User
 
Join Date: Dec 2009
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks guys for your help. good thing is that my problem is resolved.

ChronicleMaster1, your code is an alternative and it will take me time to understand and how to incorporate in my code but thanks as that helps me understand another way to do the same thing.

Imar, I included your suggestions in my web.config.

additionally, I made some changes to the gmail.com account settings to "enable POP for all the emails" and then configured the new account setup in Outlook using the gmail credentails.

together with all above, things have worked.

thanks again to all.
 
Old January 4th, 2010, 09:51 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

With Imar on the case, I'm sure your code will be up and running in no time. ;)

But please feel free to browse it when you have time or check it out for proper syntax. It is stable, live code that's been working beautifully on my webhost for a couple years now.
__________________
-------------------------

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9 p. 310 Featheriver BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 February 11th, 2009 04:44 PM
chapter 9:Creating Email messages pg310 the3musketeer BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 January 21st, 2009 03:29 AM
chapter 03 ..i am getting error messages realiest BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 July 17th, 2006 03:01 PM
chapter 03 ..i am getting error messages realiest BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 July 7th, 2006 08:17 AM
Sending email from asp page stephens1982 Classic ASP Databases 6 August 25th, 2005 01:05 PM





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