Wrox Programmer Forums
|
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 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 October 2nd, 2014, 04:49 PM
Authorized User
 
Join Date: Sep 2014
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Default chapter 9 email settings

This is my previous post moved to the correct forum.

Please help me! I can't figure out what is wrong with my code.
ContactForm.ascx.cs
Quote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.IO; // Provides access to the File class for reading the file
using System.Net.Mail; // Provides access to the various mail related classes

public partial class Controls_ContactForm : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}







protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (!string.IsNullOrEmpty(PhoneHome.Text) || !string.IsNullOrEmpty(PhoneBusiness.Text))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
protected void SendButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = File.ReadAllText(fileName);

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("[email protected]", "Sender Name");
myMessage.To.Add(new MailAddress("[email protected]", "Receiver Name"));

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




Message.Visible = true;
FormTable.Visible = false;
}
}
}


Web.config
Quote:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<pages theme="Monochrome">
<controls>
<add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx"/>
</controls>
</pages>
<compilation debug="true" targetFramework="4.0"/>
</system.web>



<appSettings>
<add key="MyEmailAddr" value="[email protected]"/>
<add key="ToEmailAddr" value="[email protected]"/>
<add key="EmailHost" value="smtp.gmail.com"/>
<add key="FromEmailAddr" value="[email protected]"/>
</appSettings>

<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network enableSsl="true"
port="587"
userName="[email protected]"
password="MYPASSWORD"
host="smtp.gmail.com"/>

</smtp>

</mailSettings>

</system.net>
</configuration>
 
Old October 6th, 2014, 03:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Please help me! I can't figure out what is wrong with my code.
And what is wrong with it? Hard to suggest something if you're not providing a problem description ;-)

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 October 12th, 2014, 10:20 PM
Authorized User
 
Join Date: Sep 2014
Posts: 15
Thanks: 2
Thanked 0 Times in 0 Posts
Default the problem

When I click to submit on the contact form everything acts as if the computer is processing the request yet no email is sent and VWD defaults to the following code:

Quote:
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
it occurs at:

Quote:
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
 
Old October 13th, 2014, 06:53 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,

Your configuration code for Gmail looks fine to me, so maybe you need to set up an application specific password as explained on page 323? You can do that here: https://accounts.google.com/DisplayUnlockCaptcha

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 December 10th, 2014, 12:25 PM
Registered User
 
Join Date: Dec 2014
Posts: 10
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 9 Email

In case you are still working on this. I had a problem where my email connection would continuously time out. I am using VS2013, though I am not sure if that is the problem.

mySmtp client would not work with the credentials set in web.config. I had to sent them manual using

mySmtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password")

The port, etc could be set in web.config but the network credentials I had to configure like this and it worked for me using gmail.

(Though with gmail you also have to follow some steps to set your account up to do this, if you try it once, google will send you an email with instructions for how to set it up properly)
The Following User Says Thank You to Harpist For This Useful Post:
hatsmccoy (February 6th, 2015)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with email from chapter 09 happyandstable BOOK: Beginning ASP.NET 4 : in C# and VB 1 February 24th, 2012 06:41 PM
Chapter 9 - Settings.bundle and xCode 4.2 dparsons BOOK: Beginning iOS 5 Application Development 0 February 23rd, 2012 05:21 PM
Chapter 9 Trying to send email out digink BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 6 March 5th, 2011 05:42 AM
Chapter 9 Email not going through dtmmaxx BOOK: Beginning ASP.NET 4 : in C# and VB 1 October 8th, 2010 02:53 AM
Changing Settings.settings when assembly is in GAC evandro.paula .NET Framework 2.0 0 September 25th, 2007 09:54 AM





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