HTML formatted text in an email in Java

前端 未结 4 470
花落未央
花落未央 2021-01-15 22:12
     try{
        String msg=\"Happy BirthDay Dear, \"+name.toUpperCase()+\"  !!! Have a Great Day. \\n \\n Thank You \\n Seva Development \";
            


        
4条回答
  •  北海茫月
    2021-01-15 22:43

    You didn't specified content type of mail. In which case it is sent in plain.

    Try setting content type

    helper.setContent(htmlMsg, "text/html;  charset=\"utf-8\"");
    

    Now when you open this mail with any email client, it will read it in html format.

    You can also set multiple formats by using MimeMultitype

    Multipart multipart = new MimeMultipart("alternative");
    BodyPart messageBodyPart;
    
    // PLAIN TEXT
    messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(textBody, "text/plain; charset=\"utf-8\"");
    multipart.addBodyPart(messageBodyPart);
    
    // HTML TEXT
    messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(htmlBody, "text/html; charset=\"utf-8\"");
    multipart.addBodyPart(messageBodyPart);
    
    message.setContent(multipart);
    

提交回复
热议问题