We help IT Professionals succeed at work.

Sending Email using ASP.NET

asp_net2
asp_net2 asked
on
Medium Priority
364 Views
Last Modified: 2012-05-12
Hello Experts,

I would like to know the best and most recoommened way to format code to use to send emails out using ASP.NET C#. I have attached what I currently use now as a method for sending out emails after a user fills out a form. However the part that I'm having trouble with is adding a value from a DB that needs emailed to a user inside of a <a href> tag. I need to be able to email a link and associate that link with the employees ID.

The value that needs added to the URL is a hiddenfield value labeled hf_usersid.
protected void SendForgotPasswordEmail()
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        //Prepare email address
        MailAddress fromAddress = new MailAddress("no-reply@test.org", "Email Name");
        MailAddress toAddress = new MailAddress(HttpUtility.HtmlEncode(txtEmail.Text));

        //Prepare the main message
        message.From = fromAddress;
        message.To.Add(toAddress);

        message.Subject = "Subject Title";
        message.IsBodyHtml = true;
        message.Body = "<html><head><title>" + "</title></head><body>" + "<p>" + "<span style=\"font-size: 16px; color: #0c9b19; font-family: Arial\"><b>Reset Your Password</b><font face='arial' color='#666666'>" + "<br /><br />" + "<span style=\"font-size: 14px; color: #006da5; font-family: Arial\"><b>Your Email:&nbsp;</b><font face='arial' color='#666666'>" + HttpUtility.HtmlEncode(txtEmail.Text) + "<br />" + "<span style=\"font-size: 14px; color: #006da5; font-family: Arial\"><b>Your Username:&nbsp;</b><font face='arial' color='#666666'>" + HttpUtility.HtmlEncode(hf_users_id.Value) + "<br />" + "<span style=\"font-size: 12px; font-style: italic; color: #de1919; font-family: Arial\"><br /><br />" + "<a " + "<span style=\"font-size: 12px; font-style: italic; color: #de1919; font-family: Arial\"><br /><br />" + "** Passwords are unretrievable. Please make sure that you remember your password or store it in a safe place. **" + "</body></html>";

        smtpClient.Host = "mail.test.org";
        smtpClient.Send(message);
    }

Open in new window

Comment
Watch Question

SAMIR BHOGAYTATeam Lead
CERTIFIED EXPERT

Commented:
Hello,

You have to create one another form and put message.Body = "Which you put here" into this form and take the values of a database into Session variables then you can take this values into the other form. And you can send the whole form as like a mail. Try this.

Commented:
Couldn't you simply get the database value and assigned it to a string and then concatenate the variable to your string?

Example
EmployeeID = GetValueFromdB

message.Body = "<html><head><title>" + EmployeeID + "</title></head><body>" + "<p>"

Author

Commented:
@samirbhogayta / PJBX,

I already have the value from the DB. I just need to add the value to the end of the URL. This is what I'm not sure about. I have no problem retrieving the data just a problem adding the value to the URL. If I add the value without the URL it's fine, it gets returned. I need it to look like the following example below.

http://test.test.org/programinfo/ghap/retrievepassword.aspx?UsersID=18

The value 18 is associated to the HiddenField value hf_usersid.

Commented:
Ok. Then try

message.Body = "<html><head><title>" + "<br><br>Click <a href='http://test.test.org/programinfo/ghap/retrievepassword.aspx?UsersID=" + EmployeeID + "'>here</a>

This should be on one line and a space between <a href.

Author

Commented:
That did not work. I don't even get a value returned to me via email.
message.Body = "<html><head><title>" + "</title></head><body>" + "<p>" + "<span style=\"font-size: 16px; color: #0c9b19; font-family: Arial\"><b>Reset Your Password</b><font face='arial' color='#666666'>" + "<br /><br />" + "<span style=\"font-size: 14px; color: #006da5; font-family: Arial\"><b>Your Email:&nbsp;</b><font face='arial' color='#666666'>" + HttpUtility.HtmlEncode(txtEmail.Text) + "<br />" + "<span style=\"font-size: 14px; color: #006da5; font-family: Arial\"><b>Your Username:&nbsp;</b><font face='arial' color='#666666'>" + HttpUtility.HtmlEncode(hf_users_id.Value) + "<br />" + "<span style=\"font-size: 12px; font-style: italic; color: #de1919; font-family: Arial\"><br /><br />" + "<a href='http://test.test.org/programinfo/ghap/retrievepassword.aspx?UsersID=" + hf_users_id + "'></a>" + "<span style=\"font-size: 12px; font-style: italic; color: #de1919; font-family: Arial\"><br /><br />" + "** Passwords are unretrievable. Please make sure that you remember your password or store it in a safe place. **" + "</body></html>";

Open in new window

Commented:
Check the file for the correct syntax
EMPURL.txt

Commented:
The key..You have to embed a HREF tag in your message body and concatenate the EmployeeID. confirm the syntax.

http://www.w3schools.com/tags/att_a_href.asp

Author

Commented:
Thank you! That worked exactly as I needed!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.