2
votes

I don't understand the issue at all. If I'm trying from main method while running as java application, then the mail is coming perfectly with proper subject and content formatted. while when I try from localhost , it comes in broken format such as

------=_Part_0_1765202668.1460463643056 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

My Content

------=_Part_0_1765202668.1460463643056--

I have added all the relevant jars(javax.mail). No matter what the content is, it'll come as such only. It's the same piece of code which works well from main methos but doesn't with local host. any ideas?

some relevant code

MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(this.from));
    if ((this.replyTo != null) && (!this.replyTo.equals("")))
        msg.setReplyTo(InternetAddress.parse(this.replyTo));
    msg.setSentDate(new Date());
    InternetAddress[] address = InternetAddress.parse(this.to);
    msg.setRecipients(Message.RecipientType.TO, address);

    if (this.cc != null) {
        InternetAddress[] address1 = InternetAddress.parse(this.cc);
        msg.setRecipients(Message.RecipientType.CC, address1);
    }

    if (this.bcc != null) {
        InternetAddress[] address2 = InternetAddress.parse(this.bcc);
        msg.setRecipients(Message.RecipientType.BCC, address2);
    }
    msg.setSubject(this.subject);

    Multipart mp = new MimeMultipart();

    MimeBodyPart mbp = new MimeBodyPart();
    mbp.setContent(this.body,"text/html;charset=utf-8");

    mp.addBodyPart(mbp);

    if (this.attachfiles != null) {
        for (Enumeration e = this.attachfiles.keys(); e.hasMoreElements();) {
            String filename = (String) e.nextElement();
            mbp = new MimeBodyPart();

            FileDataSource fds = new FileDataSource(
                    (String) this.attachfiles.get(filename));
            mbp.setDataHandler(new DataHandler(fds));
            mbp.setFileName(filename);

            mp.addBodyPart(mbp);
        }

    }

    msg.setContent(mp);

    msg.setSentDate(new Date());

    Transport.send(msg);
1
I don't get the difference between "trying from main" and "trying from localhost". Can you explain better what you are doing?Henry
By "localhost" do you mean you're running in some sort of application server? Are you sure the server is using the same version of JavaMail? What does the JavaMail debug output show?Bill Shannon

1 Answers

2
votes

There was an issue with pom.xml Turns out there was a conflict in packages. Tomcat automatically includes its own JavaMail package in the Maven build from two other Jars in the web project which was causing problem instead of importing from the standard JavaMail jar.

Just exclude the following jars

    <dependency>
    <groupId>org.apache.ws.commons.axiom</groupId>
    <artifactId>axiom-api</artifactId>
    <version>1.2.7</version>
    <exclusions>
        <exclusion>
            <artifactId>geronimo-javamail_1.4_spec</artifactId>
            <groupId>org.apache.geronimo.specs</groupId>
        </exclusion>
        <exclusion>
            <artifactId>geronimo-activation_1.1_spec</artifactId>
            <groupId>org.apache.geronimo.specs</groupId>
        </exclusion>
    </exclusions>
    </dependency>