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

前端 未结 5 1998
伪装坚强ぢ
伪装坚强ぢ 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: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);
    

提交回复
热议问题