Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

Sending Email using ASP.NET

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

Avatar of SAMIR BHOGAYTA
SAMIR BHOGAYTA
Flag of India image

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.
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>"
Avatar of Brian

ASKER

@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.
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.
Avatar of Brian

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of PJBX
PJBX
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Brian

ASKER

Thank you! That worked exactly as I needed!