We help IT Professionals succeed at work.
Get Started

Sending Email using ASP.NET

asp_net2
asp_net2 asked
on
369 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
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE