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

前端 未结 5 1997
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 07:59

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

相关标签:
5条回答
  • 2021-02-19 08:27

    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

    0 讨论(0)
  • 2021-02-19 08:33

    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

    0 讨论(0)
  • 2021-02-19 08:41

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

    0 讨论(0)
  • 2021-02-19 08:42

    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);
    
    0 讨论(0)
  • 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);
         }
      }
    
    0 讨论(0)
提交回复
热议问题