Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I put a HTML link Inside an email body?

I have an application thats can send mails, implemented in Java. I want to put a HTML link inside de mail, but the link appears as normal letters, not as HTML link... How can i do to inside the HTML link into a String? I need special characters? thank you so much

Update: HI evereybody! thanks for oyu answers! Here is my code:

public static boolean sendMail(Properties props, String to, String from,
          String password, String subject, String body)
{
    try
    {
        MimeBodyPart mbp = new MimeBodyPart(); 
        mbp.setContent(body, "text/html"); 
        MimeMultipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(mbp); 



        // Preparamos la sesion
        Session session = Session.getDefaultInstance(props);

        // Construimos el mensaje
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setContent(multipart);
        message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(to));
        message.setSubject(subject);
        message.setText(body);

        // Lo enviamos.
        Transport t = session.getTransport("smtp");
        t.connect(from, password);
        t.sendMessage(message, message.getAllRecipients());

        // Cierre.
        t.close();
        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return false;
    }
}

And here the body String:

        String link = "<a href=\"WWW.google.es\">ACTIVAR CUENTA</a>";

But in the received message the link appears as the link string, not as HTML hyperlink! I don't understand what happens...

Any solution?

like image 378
Rafa Avatar asked Mar 30 '11 21:03

Rafa


People also ask

How do you put a link in the body of an email?

Insert a hyperlinkIn a message, position the cursor in the message body where you want to add a link. On the Message tab, click Hyperlink. In the Link box, type the address for the link. In the Text box, type the text that you want to appear in your message.

Can I add link in body?

A <link> element can occur either in the <head> or <body> element, depending on whether it has a link type that is body-ok. For example, the stylesheet link type is body-ok, and therefore <link rel="stylesheet"> is permitted in the body.


1 Answers

Adding the link is as simple as adding the <a href="..">text</a> inside the string. You should set your email to support html (it depends on the library you are using), and you should not escape your email content before sending it.

Update: since you are using java.mail, you should set the text this way:

message.setText(body, "UTF-8", "html");

html is the mime subtype (this will result in text/html). The default value that is used by the setText(string) method is plain

like image 146
Bozho Avatar answered Oct 02 '22 06:10

Bozho