Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Mail - HTML email not working showing as plain text  RSS feed

 
Rahul Chaitanya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am using the below code and despite multiple attempts the email is not being shown in "html" format but is always being showing as plain text format:



Output of the above code (<h1> tag is not getting parsed and is being displayed as a plain text): <h1> test email </h1>

Also tried the following options but none of them actually worked:



  • Same problem with this approach as well:
    message.setContent(msgData, "text/html; charset=utf-8");



  • MimeBodyPart htmlPart = new MimeBodyPart();
    String htmlContent = "<html><h1>Hi</h1><p>Nice to meet you!</p></html>";
    htmlPart.setContent(htmlContent, "text/html");


  • Any advice on this will be greatly appreciated.




     
    Paul Clapham
    Sheriff
    Posts: 22374
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Start with the JavaMail FAQ page from Oracle. One of the questions there is "How do I send HTML mail?" which seems like a good start for your search.
     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I tried the oracle mail api that you mentioned but still I am seeing the same issue, that the <html> is being displayed in the form of plain text itself and pasting the logs below:


    Loading javamail.default.providers from jar:/javax/mail/mail/1.4/mail-1.4.jar!/META-INF/javamail.default.providers
    DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Sun Microsystems, Inc, version=null
    DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Sun Microsystems, Inc, version=null
    DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Sun Microsystems, Inc, version=null
    DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Sun Microsystems, Inc, version=null
    DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Sun Microsystems, Inc, version=null
    DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Sun Microsystems, Inc, version=null
    Loading javamail.default.providers from jar:file:/C:/FAST/fastas/3.3.2/default/fcBase/repo/com/sun/mail/javax.mail/1.5.2/javax.mail-1.5.2.jar!/META-INF/javamail.default.providers
    DEBUG: loading new provider protocol=imap, className=com.sun.mail.imap.IMAPStore, vendor=Oracle, version=null
    DEBUG: loading new provider protocol=imaps, className=com.sun.mail.imap.IMAPSSLStore, vendor=Oracle, version=null
    DEBUG: loading new provider protocol=smtp, className=com.sun.mail.smtp.SMTPTransport, vendor=Oracle, version=null
    DEBUG: loading new provider protocol=smtps, className=com.sun.mail.smtp.SMTPSSLTransport, vendor=Oracle, version=null
    DEBUG: loading new provider protocol=pop3, className=com.sun.mail.pop3.POP3Store, vendor=Oracle, version=null
    DEBUG: loading new provider protocol=pop3s, className=com.sun.mail.pop3.POP3SSLStore, vendor=Oracle, version=null
    DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@6a70122; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
    DEBUG SMTP: useEhlo true, useAuth false
    DEBUG SMTP: trying to connect to host "mailhost.testdomain.net", port 25, isSSL false
    220 se2.svr.bankone.net ESMTP Sendmail Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1; Fri, 3 Jun 2016 21:28:06 GMT
    DEBUG SMTP: connected to host "mailhost.testdomain.net", port: 25

    EHLO L3SR7P12
    s250-se2.svr.bankone.net Hello [10.181.201.114], pleased to meet you
    250-ENHANCEDSTATUSCODES
    250-PIPELINING
    250-8BITMIME
    250-SIZE 41943040
    250-ETRN
    250-STARTTLS
    250-DELIVERBY
    250 HELP
    DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
    DEBUG SMTP: Found extension "PIPELINING", arg ""
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "SIZE", arg "41943040"
    DEBUG SMTP: Found extension "ETRN", arg ""
    DEBUG SMTP: Found extension "STARTTLS", arg ""
    DEBUG SMTP: Found extension "DELIVERBY", arg ""
    DEBUG SMTP: Found extension "HELP", arg ""
    DEBUG SMTP: use8bit false
    MAIL FROM:<test@test.com>
    250 2.1.0 <test@test.com>... Sender ok
    RCPT TO:<test@test.com>
    t250 2.1.5 <test@test.com>... Recipient ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP: test@test.com
    DATA
    354 Enter mail, end with "." on a line by itself

    <HTML>
    <HEAD>
    <TITLE>
    test
    </TITLE>
    </HEAD>
    <BODY>
    <h1>test</h1>


    </BODY>
    </HTML>

    .
    250 2.0.0 u53LS6QQ003721 Message accepted for delivery
    QUIT

    Mail was sent successfully.
     
    Paul Clapham
    Sheriff
    Posts: 22374
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And no matter what e-mail address you send that message to, and no matter what e-mail client you use to read it, the same thing happens?
     
    Ron McLeod
    Saloon Keeper
    Posts: 1561
    222
    Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It might be that you are missing the Content-Type: text/html header.


     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Can you please, share the code?

    I tried both the below options but still the same issue:



    Thank you.
     
    Dave Tolls
    Ranch Hand
    Posts: 2818
    30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How are you testing this?
    Essentially Paul's question...does this happen on a number of different email recipients, including different hosts, applications, etc.
     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Paul,

    Yes, no matter to whom I send the email or irrespective of the email clients, I am seeing the same issue.

    Thanks.
     
    K. Tsang
    Bartender
    Posts: 3648
    16
    Firefox Browser Java Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to use the MimeBodyPart and MimeMultipart for the email body to control whether it's plain text or html or whatever. The UTF-8 encoding can also be set.

     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is the response, I am getting with the above code:


    ------=_Part_0_735891530.1465314350219
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 7bit

    some body
    ------=_Part_0_735891530.1465314350219--


    Still no luck
     
    Ron McLeod
    Saloon Keeper
    Posts: 1561
    222
    Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rahul Chaitanya wrote:.. no matter to whom I send the email or irrespective of the email clients, I am seeing the same issue.

    Rahul -- what email client are you using and on what platform?
     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    I am using Microsoft outlook on windows machine. Let me know, if I need to provide any more information?

    Thanks.
     
    Ron McLeod
    Saloon Keeper
    Posts: 1561
    222
    Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rahul Chaitanya wrote:Can you please, share the code?

    Well .. I just took your original code, changed the SMTP host and the To/From addresses, and ran it. Using Outlook, I did see the HTML rendered (even when the HTML wasn't a proper document).



    My email server is sendmail 8.13; client is MS Outlook 14.0 (part of Office 2010).
     
    Rahul Chaitanya
    Ranch Hand
    Posts: 39
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. Fixing the different MAIL related jar file in the classpath helped to fix the issue.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!