By Tim Tuckle

2011-01-31 16:39:05 8 Comments

I am trying to send an email with Java, I am using apache commons email library.

I cannot achieve to send an email with a body as HTML and an attachment as PDF (or any file type)

If I use EmailAttachment() and add it to an HtmlEmail object, my mail looks like with two attachment. First one is for HTML, second is for PDF.

Is there any way to do that?

Thank you very much!


@Tim Tuckle 2011-02-03 17:09:01

I tried apache commons mail v1.2 instead of 1.1.

It works!?

Andrej, by the way many thanks for your kindly help.

@Andrzej Doyle 2011-01-31 16:56:48

It sounds like relatively normal behaviour for a message that's being sent as both text and HTML, and/or a mail client (at the receiving end) that prefers text emails. I suspect that this is due to the behaviour of the client, which you won't be able to change (but on the plus side all HTML emails would appear like this).

The thing is, an HTML email (with a textual component) really is a multipart message, with the HTML content as one of the "extra" parts. All you're actually sending in the email from the server side is a bunch of text, and it's up to the receiving mail client to decide how to display it. In that respect, it is not wrong for the client to display your HTML as an attachment - just like it is not wrong for a smart client to infer that the HTML isn't a "real" attachment and activate some kind of toggle between text and HTML (rather than displaying it as an attachment).

If you're convinced that the client would normally treat HTML in this smart way, then:

  1. You'll have to mention which client you're using to check, because this isn't really an issue with the sending per se; and
  2. You might want to take a look at the raw source of email that "works", and your email that doesn't, in order to determine what the critical differences are that trigger the different rendering modes. Depending on the client software, this could be just about anything - but I'd pay particular attention to part MIME types and charsets.

Related Questions

Sponsored Content

74 Answered Questions

88 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

95 Answered Questions

[SOLVED] How to validate an email address in JavaScript

43 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

33 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

58 Answered Questions

[SOLVED] How to create a memory leak in Java?

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4113191 View
  • 3550 Score
  • 66 Answer
  • Tags:   java random integer

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

15 Answered Questions

[SOLVED] Efficiency of Java "Double Brace Initialization"?

Sponsored Content