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

风格不统一 提交于 2019-12-12 08:21:04

问题


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?


回答1:


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




回答2:


I'm just going to answer in case this didn't work for someone else.
I tried Bozho's method and for some reason the email wouldn't send when I did the setText on the message as a whole.

I tried

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setContent(body, "text/html"); 

but this came as an attachment in Outlook instead of in the usual text. To fix this for me, and in Outlook, instead of doing the mbp.setContent and message.setText, I just did a single setText on the message body part. ie:

MimeBodyPart mbp = new MimeBodyPart(); 
mbp.setText(messageBody,"UTF-8", "html");

With my code for the message looking like this:

MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
for(String str : to){
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(str));
}        
message.setSubject(subject);
// Create the message part 
MimeBodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(messageBody,"UTF-8","html");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);



回答3:


Appending "http://" before the URL worked for me.




回答4:


We can create html link in the email body by using java code.In my case I am developing reset password where I should create link and send to the user through the mail.you will create one string.With in a string you type all the url.If you add the http to the that .it behaves like link with in the mail.

Ex:String mailBody ="http://localhost:8080/Mail/verifytoken?token="+ token ;

you can send some value with url by adding query string.Her token has some encrypted value.

put mailBody in your mail body parameter. ex": "Hi "+userdata.getFirstname()+ "\n\n You have requested for a new password from the X application. Please use the below Link to log in."+ "\n\n Click on Link: "+mailBody);

The above is the string that is parameter that you have to pass to your mail service.Email service takes parameters like from,to,subject,body.Here I have given body how it should be.you pass the from ,to,subject values according to your cast




回答5:


you can do right way it is working for me.

 public  class SendEmail
 {
   public void getEmail(String to,String from, String userName,String password,Properties props,String subject,String messageBody)
  {
      MimeBodyPart mimeBodyPart=new MimeBodyPart();
      mimeBodyPart.setContent(messageBody,"text/html");
      MimeMultipart multipart=new MimeMultipart();
      multipart.addBodyPart(mimeBodyPart);
      Session session=Session.getInstance(props,new Authenticator()
        {
          protected PasswordAuthentication getPasswordAuthentication()
           {
              return new PasswordAuthentication(userName,password);
           }
       });
         try{
              MimeMessage message=new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.setContent(multipart);
              message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
             message.setSubject("Have You got Mail!");
             message.setText(messageBody,"UTF-8","html");
             Transport.send(message);
           }
           catch(MessagingException ex){System.out.println(ex)}
      public static void main(String arg[]){
         SendEmail sendEmail=new SendEmail();
           String to = "XXXXXXX@gmail.com";      
           String from = "XXXXXXXX@gmail.com";
           final String username = "XXXXX@gmail.com";
           final String password = "XXXX";
            String subject="Html Template";

          String body = "<i> Congratulations!</i><br>";
          body += "<b>Your Email is working!</b><br>";
          body += "<font color=red>Thank </font>";
          String host = "smtp.gmail.com";
          Properties props = new Properties();
          props.put("mail.smtp.auth", "true");
          props.put("mail.smtp.starttls.enable", "true");
          props.put("mail.smtp.host", host);
          props.put("mail.smtp.port", "587");
          sendEmail.getEmail(to,from,username,password,props,subject,body);
     }
  }


来源:https://stackoverflow.com/questions/5492869/how-can-i-put-a-html-link-inside-an-email-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!