Sheraz
Sheraz

Reputation: 567

Send a link in mail in MVC

I want to send a link like this here using email but i am unable to do this. i tried following code.

String body = "<a href=\"google.com\">Here</a>";
                var client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential("[email protected]", "password"),
                    EnableSsl = true
                };

                client.Send("[email protected]", emailAddress, "Password Recovery", body);

But in email i get this.

<a href="google.com"> Here </a>

Instead of this Here

Upvotes: 0

Views: 3144

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You have to tell that it is Html, you can use MailMessage class to do it:

 MailMessage msg = null;
 var client = new SmtpClient("smtp.gmail.com", 587)

 try
 {
    msg = new MailMessage("[email protected]",
                          "[email protected]", 
                          "Subject",
                          "Message");
    msg.IsBodyHtml = true;
    client.Send(msg);

 }

This article (Send Html Formatted Emails in asp.net using C#) will also help you.

Upvotes: 0

teo van kot
teo van kot

Reputation: 12491

You should specify that body is Html. Like this:

    String body = "<a href=\"google.com\">Here</a>";
    var client = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new NetworkCredential("[email protected]", "password"),
        EnableSsl = true
    };

    MailMessage message = new MailMessage("[email protected]", emailAddress, "Password Recovery",body )
    message.IsBodyHtml = true;

    client.Send(message);

Also, it's not MVC question at all...

Upvotes: 1

offi
offi

Reputation: 380

You need to create MailMessage with IsBodyHtml = true, then send it via SmtpClient

Upvotes: 0

Related Questions