0
votes

I am sending multi part email which is having text/plain and text/html but when i am getting mail in my outlook html content is coming as attachment and text/plain is coming in the body. i want both in the body.

pom.xml configuration is this

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.2</version>
    </dependency>

and java code is

public static void main(String[] args) throws Exception {

    Properties props = new Properties();
    props.put("mail.smtp.host", sSMTPServer);
    props.put("mail.smtp.port", 25);
    Session session = null;

    session = Session.getInstance(props, null);
    MimeMessage msg = new MimeMessage(session);


    Multipart mainMultipart = new MimeMultipart("mixed");
    Multipart htmlAndTextMultipart = new MimeMultipart("alternative");


    MimeBodyPart BodyPart = new MimeBodyPart();
    BodyPart.setText(Header);
    htmlAndTextMultipart.addBodyPart(BodyPart);


    MimeBodyPart BodyPart1 = new MimeBodyPart();
    BodyPart1.setContent(Body, "text/html; charset=utf-8");
    htmlAndTextMultipart.addBodyPart(BodyPart1);

    for (int i = 0; i < htmlAndTextMultipart.getCount(); i++) {
        mainMultipart.addBodyPart(htmlAndTextMultipart.getBodyPart(i));
    }
    msg.setContent(mainMultipart);

    InternetAddress[] from = InternetAddress.parse("[email protected]");
    InternetAddress[] toList = InternetAddress.parse(to);
    msg.addFrom(from);
    msg.addRecipients(Message.RecipientType.TO, toList);
    msg.setSubject("Multipart_Testing");

    Transport transport = session.getTransport("smtp");
    transport.connect(sSMTPServer, 25, null,
            null);
    transport.sendMessage(msg, toList);
    System.out.println("Sent");
    transport.close();
}

problem is only with outlook that html content is not coming in the body

mail snippet of gmail

and in outlook all contents are not getting rendered like gmail instead coming as attachment
mail snippet of outlook

1

1 Answers

0
votes

You don't get to control how mailers display your message, and different mailers will display the same message differently. Your best bet is to put all your content into a single html part, and stick to pretty basic html.

Oh, and JavaMail 1.4.2 is very old, you should upgrade to the current version if possible.