p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   SMTP question (http://p2p.wrox.com/showthread.php?t=86513)

vbboyd January 31st, 2012 03:25 PM

SMTP question
 
Quick question: I noticed in chapter 9 and 10 of your book that you have the email addresses hard coded in the Web.config files and in the .ascx.cs page of the the contact form. Is there anyway at all that these email addresses in both the web.config and the in the code behind of the contact.ascx form can be created dynamically without having to be hard coded into the forms and web.config files themselves.

Thank you once again.

Imar January 31st, 2012 04:56 PM

Hi there,

What do you mean with this:

Quote:

can be created dynamically without having to be hard coded into the forms and web.config files themselves.
You need to store the addresses somewhere, no?

Chapter 19 shows how to move the addresses from the Code Behind into the web.config so you have a central location to manage them.

Cheers,

Imar

vbboyd January 31st, 2012 05:15 PM

Quote:

Originally Posted by Imar (Post 280949)
Hi there,

What do you mean with this:


You need to store the addresses somewhere, no?

Chapter 19 shows how to move the addresses from the Code Behind into the web.config so you have a central location to manage them.

Cheers,

Imar

[code]
I am sorry I should have been more specific. In your web.config file you have the following stuff:
For example:
<mailSettings>
<smtp deliveryMethod="Network" from="Joe@aol.com">
<network enableSsl="true" userName="Joe@aol.com" password="123ABC" host="smtp.aol.com" port="587"/>
<specifiedPickupDirectory pickupDirectoryLocation="C:\TempMail" />
</smtp>
</mailSettings>
Then .cs file of the contactform.ascx you have:
myMessage.From = new MailAddress("Joe@aol.com", "Joe Norrigan");
myMessage.To.Add(new MailAddress("LoriL@yahoo.com", "Lori Lopez"));

As you can see up above the email addresses are all hard coded into the web.config file and the code behind of the .ascx file. Is there anyway that the information for the webaddresses not be hard coded into those files or a central location, but are instead created dynamically on the fly, by the user typing the information into a textbox or a text area instead of them being stored in a different centralized location? I hope that helps me clarify things just a little bit better.

As always: Thanks again!

Imar January 31st, 2012 05:26 PM

Sure. Just replace this:

Code:

myMessage.From = new MailAddress("Joe@aol.com", "Joe Norrigan");
with this:

Code:

myMessage.From = new MailAddress(txtEmailAddress.Text, txtEmailName.Text);
You can't programmatically override the from in the smtp settings, but it only serves as a default. By setting the From in code as shown above, the from in the web.config is not used.

I personally don't like this though. When an e-mail address is not valid, your e-mail may not get sent correctly. It also creates opportunities for spammers / hackers to send e-mails out of someone else's name.

Cheers,

Imar

vbboyd January 31st, 2012 05:28 PM

SMTP Question
 
[code]

I guess in other words what I am trying to say is there anyway that a user can type in a text box their email address and then put that into the code instead? The example in the book is hard coded, is:
Message.from = new.MailAddress("you@yourprovider.com, "Sender Name");
Message.To.Add(new MailAddress("you@yourprovider.com,"Receiver Name");
Would it be possible to enter the sender and receiver information into a textbox or textarea and then put them into the "you@yourprovider.com", Receivername) dynamically inside the code. Because I don't see where the text information off of the webform is being subsitituted into the to segment of the address up above. Maybe it is just me. Maybe I am just confused. I just don't see how the email address in the text box is being substituted into the code example up above.

vbboyd January 31st, 2012 06:57 PM

SMTP question
 
Quote:

Originally Posted by Imar (Post 280952)
Sure. Just replace this:

Code:

myMessage.From = new MailAddress("Joe@aol.com", "Joe Norrigan");
with this:

Code:

myMessage.From = new MailAddress(txtEmailAddress.Text, txtEmailName.Text);
You can't programmatically override the from in the smtp settings, but it only serves as a default. By setting the From in code as shown above, the from in the web.config is not used.

I personally don't like this though. When an e-mail address is not valid, your e-mail may not get sent correctly. It also creates opportunities for spammers / hackers to send e-mails out of someone else's name.

Cheers,

Imar

Code:

What I was kind of actually wondering about was the To: address. As I am sure you are aware, (and please correct me if I am wrong) the To:adress is hard coded into the ascx.cs file with the following line of code:
myMessage.To.Add(new MailAddress("youraddress@yourprovider.com", "Receiver Name"));
That then beggars the hypothetical question of: lets just say for instance I am Greg Norigan with an email address of GregN@aol.com and I go to your website of ImarProductions.com to sign up for your email newsletter that has all the latest, up to date information about concert dates, concert reviews, new musical CD releases etc, etc, and I put my name and email address in the textboxes and then the "to:" field of the email address is hardcoded to go to Lori Lopez with an email address at LoriL@aol.com, inside the ascx.cs file then how is that beneficial to you and your website? The email then gets sent to LoriL@aol.com instead of GregN@aol.com does it not? This is where my confusion basically comes from. That is what I really don't understand.


Imar January 31st, 2012 07:16 PM

First of all, you can do the same thing with the To address to make it dynamic.

But obviously it all depends who the target of the e-mail is. In the book's example, it's a contact form so it makes a lot of sense to send this to the site's owner / manager and not to the person sending the message (as he would be talking to himself).

For a newsletter you do indeed need the user's address. If you send out the information right away, just take the To from the form. For regular newsletters, you typically store the addresses in a database and use them in the To field when you send out the newsletter.

Cheers,

Imar

vbboyd January 31st, 2012 09:36 PM

SMTP Question
 
Quote:

Originally Posted by Imar (Post 280960)
First of all, you can do the same thing with the To address to make it dynamic.

But obviously it all depends who the target of the e-mail is. In the book's example, it's a contact form so it makes a lot of sense to send this to the site's owner / manager and not to the person sending the message (as he would be talking to himself).

For a newsletter you do indeed need the user's address. If you send out the information right away, just take the To from the form. For regular newsletters, you typically store the addresses in a database and use them in the To field when you send out the newsletter.

Cheers,

Imar

All I can say is it would have been nice if you had included in your book how to do newsletters using ASP.NET 4.0 and SQL Server.

Imar February 1st, 2012 08:05 AM

Quote:

All I can say is it would have been nice if you had included in your book how to do newsletters
That's not a whole lot ;-)

The thing is: there are potentially hundreds of other topics I haven't been able to cover. I already had troubles keeping the book under 800 pages. Also, building a Newsletter tool is not a small undertaking and is probably better off in the follow up Pro edition of this book.

That all said, you'll find all the information in the book to build this yourself. Storing and retrieving data from a database is covered extensively (e.g. Signup and sending out the newsletters). The chapter on mail shows how to send out e-mails and the chapter on deployment shows how to make the addresses dynamic (just as I showed in this thread). Adding it all together would give you a good start to newsletter type of functionality.

I am in the process of updating the book for the next version of .NET, so if you absolutely think newsletters is a topic that should be covered, feel free to post a list of topics that you'd like to see covered.

Cheers,

Imar

vbboyd February 1st, 2012 02:02 PM

Quote:

Originally Posted by Imar (Post 280975)
That's not a whole lot ;-)

I am in the process of updating the book for the next version of .NET, so if you absolutely think newsletters is a topic that should be covered, feel free to post a list of topics that you'd like to see covered.

Cheers,

Imar

Just curious......
So you say there is a new version of .NET in the works? Any time table for a possible release date that you might be aware of? Is there a beta version already available?

Thanks once again.

Imar February 1st, 2012 06:51 PM

Quote:

So you say there is a new version of .NET in the works?
Yes, or I wouldn't be writing about it ;-)
Quote:

Any time table for a possible release date that you might be aware of?
Nope.
Quote:

Is there a beta version already available?
Yep: http://www.google.nl/#sclient=psy-ab...w=1366&bih=644


Cheers,

Imar


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

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