Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert a link in to a email send using c#

Tags:

c#

I develop a program to send emails automatically using c#, and I want to insert a link to a web site to that email. How can I do it?

public bool genarateEmail(String from, String to, String cc, String displayName, 
                          String password, String subjet, String body)
{
    bool EmailIsSent = false;

    MailMessage m = new MailMessage();
    SmtpClient sc = new SmtpClient();
    try
    {
        m.From = new MailAddress(from, displayName);
        m.To.Add(new MailAddress(to, displayName));
        m.CC.Add(new MailAddress("[email protected]", "Display name CC"));

        m.Subject = subjet;
        m.IsBodyHtml = true;
        m.Body = body;


        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.Credentials = new
        System.Net.NetworkCredential(from, password);
        sc.EnableSsl = true;
        sc.Send(m);

        EmailIsSent = true;

    }
    catch (Exception ex)
    {
        EmailIsSent = false;
    }

    return EmailIsSent;
}

I want to send a link through this email. How should I add it to email?

like image 631
user2234111 Avatar asked Apr 02 '13 00:04

user2234111


People also ask

How do I attach a link to a email in an email?

On the Insert tab, click Link or Hyperlink. Under Link to, click E-mail Address. Either type the email address that you want in the E-mail address box, or select an email address in the Recently used e-mail addresses list. If you want to change the link text, in the Text to display box, type the text.


3 Answers

You should be able to just add the mark-up for the link in your body variable:

body = "blah blah <a href='http://www.example.com'>blah</a>";

You shouldn't have to do anything special since you're specifying your body contains HTML (m.IsBodyHtml = true).

like image 175
David Hoerster Avatar answered Oct 18 '22 21:10

David Hoerster


 String body = "Your message : <a href='http://www.example.com'></a>"
 m.Body = body;
like image 33
Gayashan Avatar answered Oct 18 '22 22:10

Gayashan


Within the body. This will require that the body be constructed as HTML so the that a or can be used to render your link. You can use something like StringTemplate to generate the html including your link.

like image 29
James Moring Avatar answered Oct 18 '22 22:10

James Moring