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 .
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.
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.
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:
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?
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.
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.: