 |
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
|
|
|
|
|

March 6th, 2012, 05:57 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
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(
"[email protected]", "From Me");
MailAddress toAddress = new MailAddress(
"[email protected]", "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.
|
|

March 6th, 2012, 07:34 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
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(
"[email protected]", "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.
|
|

March 6th, 2012, 07:45 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
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>";
|
|

March 6th, 2012, 09:37 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
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("[email protected]", "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(" [email protected]") 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!
|
|

March 6th, 2012, 10:12 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
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:
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.
|
|

March 7th, 2012, 08:49 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
 |
|