Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 6th, 2012, 04:57 PM
Friend of Wrox
Points: 1,858, Level: 17
Points: 1,858, Level: 17 Points: 1,858, Level: 17 Points: 1,858, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 404
Thanks: 13
Thanked 6 Times in 6 Posts
Default SMTP Email problems

using the following code in the code behind file does not work:

Code:
  protected void SendEmail(object sender, EventArgs e)
  {
    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();
    try
    {
      // Prepare two email addresses 
      MailAddress fromAddress = new MailAddress(
          "youraddress@aol.com", "From Me");
      MailAddress toAddress = new MailAddress(
          "ImarProductions@gmail.com", "To Imar");
      // Prepare the mail message
      message.From = fromAddress;
      message.To.Add(toAddress);
      message.Subject = "Testing!";
      message.Body = "This is the body of a sample message";
      // Set server details
      smtpClient.Host = "aol.com";
      // For SMTP servers that require authentication
      smtpClient.Credentials = new System.Net.NetworkCredential(
          "MyUserId", "MyPassword", "587");
      // Send the email
      smtpClient.Send(message);
      // Inform the user
      statusLabel.Text = "Email sent.";
    }
    catch (Exception ex)
    {
      // Display error message
      statusLabel.Text = "Coudn't send the message!";
    }
  }
Of course the to and send fields are totally generic along with the UserID and Password. Can you do this from the Code behind file in .NET? When I run this nothing gets sent nor does the exception get thrown where it says the email was not sent either. Can it be done this way or do you have to use the Web.config file for all of this for like the SMTP mail network credentials? AOL is the dummy email accounts that I use to set up send email messages and gmail is the dummy emails accounts that I set up to receive messages from AOL.
Reply With Quote
  #2 (permalink)  
Old March 6th, 2012, 06:34 PM
Friend of Wrox
Points: 1,858, Level: 17
Points: 1,858, Level: 17 Points: 1,858, Level: 17 Points: 1,858, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 404
Thanks: 13
Thanked 6 Times in 6 Posts
Default No matter what I try this code will not work also!

Code:
protected void sendNewsletterButton_Click(object sender, EventArgs e)
  {
    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();
    // Try to send the message
    try
    {
      // Prepare two email addresses 
        MailAddress fromAddress = new MailAddress(
                  "youraddress@aol.com", "From Imar");
     MailAddress toAddress = new MailAddress(toTextBox.Text);
      // Prepare the mail message
      
      // Prepare the mail message
      message.From = fromAddress;
      message.To.Add(toAddress);
      
      message.From = fromAddress;
      message.To.Add(toAddress);
      message.Subject = subjectTextBox.Text;
     message.IsBodyHtml = true;
      message.Body =
       "<html><head><title>" +
       HttpUtility.HtmlEncode(subjectTextBox.Text) +
        HttpUtility.HtmlEncode(introTextBox.Text) + "</p>" +
       "<p>Employee of the month: " +
        HttpUtility.HtmlEncode(ConcertPeformersTextBox.Text) + "</p>" +
       "<p>This months featured event: " +
        HttpUtility.HtmlEncode(ConcertEventTextBox.Text) + "</p>" +
        "</body></html>";
       Set server details
      SmtpClient mySmtpClient = new SmtpClient();
      mySmtpClient.Send(message);
      // Inform the user
      resultLabel.Text = "Email sent!<br />";
    }
    catch (Exception ex)
    {
      // Display error message
      resultLabel.Text = "Couldn\'t send the message!";
    }
  }
}
When I click on the button nothing happens. No email gets sent! Nothing, nada, zip happens. The USERID and Passwords are stored in the web.config file. Nobody in the world could make this code work they wanted to. This is a real world example taking info off of a form that a user can fill out and then send to anybody they want to for like newsletters and what have you.
Reply With Quote
  #3 (permalink)  
Old March 6th, 2012, 06:45 PM
Friend of Wrox
Points: 1,858, Level: 17
Points: 1,858, Level: 17 Points: 1,858, Level: 17 Points: 1,858, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 404
Thanks: 13
Thanked 6 Times in 6 Posts
Default

I also wanted to include a concert pic at the top of the newsletter of the concert performer in addition to the concert event and the performance information using the following code:

Code:
"<html><head><title>" +
        HttpUtility.HtmlEncode(subjectTextBox.Text) +
        "</title></head><body>" +
     "<img src="/Images/BonJovi.jpg" />" +
 "<html><head><title>" +
       HttpUtility.HtmlEncode(subjectTextBox.Text) +
        HttpUtility.HtmlEncode(introTextBox.Text) + "</p>" +
       "<p>Concert Event: " +
        HttpUtility.HtmlEncode(ConcertPeformersTextBox.Text) + "</p>" +
       "<p>This months featured concert event: " +
        HttpUtility.HtmlEncode(ConcertEventTextBox.Text) + "</p>" +
        "</body></html>";
Reply With Quote
  #4 (permalink)  
Old March 6th, 2012, 08:37 PM
Friend of Wrox
Points: 1,858, Level: 17
Points: 1,858, Level: 17 Points: 1,858, Level: 17 Points: 1,858, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 404
Thanks: 13
Thanked 6 Times in 6 Posts
Default All I am really trying to do is.....

Given your code from chapter 9
Code:
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
      string mailBody = File.ReadAllText(fileName);
      string emailsend = EmailAddress.Text;

      mailBody = mailBody.Replace("##Name##", Name.Text);
      mailBody = mailBody.Replace("##Email##", EmailAddress.Text);
      mailBody = mailBody.Replace("##HomePhone##", PhoneHome.Text);
      mailBody = mailBody.Replace("##BusinessPhone##", PhoneBusiness.Text);
      mailBody = mailBody.Replace("##Comments##", Comments.Text);

      MailMessage myMessage = new MailMessage();
      myMessage.Subject = "Response from web site";
      myMessage.Body = mailBody;

      myMessage.From = new MailAddress("ImarProductions", "ImarProductions @Imar.com");
      myMessage.To.Add(new MailAddress("anybodysEmail@yahoo.com", "AnybodyAtYahoo"));

      SmtpClient mySmtpClient = new SmtpClient();
      mySmtpClient.Send(myMessage);
I would like take the value from the emailAddress.text textbox and put it into the myMessage.To.Add.(new MailAdress("anybodysEmail@yahoo.com") so that it is not hard coded into the code itself but is rather added dynamically by the user putting the email to address into the EmailAddress.Text textbox. How do you do something like that? Or better yet how would you do this for all of the textboxes that the user is putting into the textboxes? Basically how would you build the string without using the ContactForm.txt? But is rather is built by the user putting that information himself so that the whole thing behaves more like a email newsletter rather than a contact form?

Thank you very much!
Reply With Quote
  #5 (permalink)  
Old March 6th, 2012, 09:12 PM
Friend of Wrox
Points: 1,858, Level: 17
Points: 1,858, Level: 17 Points: 1,858, Level: 17 Points: 1,858, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 404
Thanks: 13
Thanked 6 Times in 6 Posts
Default How is it done?

Sorry I am so very confused here. My bad. I probably did not make myself too clear. All I want to do is to make it so that "to address of the code is not hard coded like you have done in your code here:
Code:
 myMessage.To.Add(new MailAddress("anybodysEmail@yahoo.com", "AnybodyAtYahoo"));
but rather replace that value with the value that comes from the text box that user types in themselves. And if it all possible add to the top of the email that is sent out an image of the concert perfomer like say for instance BonJovi.jpg that will be performing at the upcoming concert that is saved in the Images/Preformer folder. So this way I can send out lets say concert information to people that are on my newsletter email list.

Thank you once again.
Reply With Quote
  #6 (permalink)  
Old March 7th, 2012, 07:49 AM
Imar's Avatar
Wrox Author
Points: 66,617, Level: 100
Points: 66,617, Level: 100 Points: 66,617, Level: 100 Points: 66,617, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,123
Thanks: 65
Thanked 1,386 Times in 1,366 Posts
Default

Quote:
All I want to do is to make it so that "to address of the code is not hard coded like you have done in your code here:
Didn't we already discuss this here?

SMTP question

Quote:
And if it all possible add to the top of the email that is sent out an image of the concert perfomer
To understand how to do this, you need to realize where the e-mail is being processed. At the recipient's machine, not at the server. Therefore, something like /Images/BonJovi.jpg won't work as it will look for that image on the user's local drive. Instead, use a full domain path. E.g.:

http://www.yourdomain.com/Images/BonJovi.jpg

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!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP Email question snufse ASP.NET 2.0 Basics 2 July 9th, 2009 11:08 AM
SMTP Email Error Handling Ron Howerton Pro Visual Basic 2005 1 July 12th, 2007 08:32 AM
Problems with SMTP asf_fmi BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 January 20th, 2007 12:32 AM
Sending EMail thru SMTP server in C# - Most of the peri C# 1 December 20th, 2004 08:33 PM
mail() is not sending email on localhost SMTP anshul PHP How-To 5 August 4th, 2004 06:45 AM



All times are GMT -4. The time now is 02:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.