0


I am trying to send an email in Java. What I am doing now is described in the following:

my_result = my_function();

// header
String notificationSubject = "Test email";

// content
final MimeMultipart notificationContent = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Plain text.");
notificationContent.addBodyPart(messageBodyPart);

... code to send the email ...

At the moment, the body of the email should be composed only by the text: "Plain text". I would like to write a more complex email like the following one:

Dear all,
the result of the operation was not successful. The following value caused a problem:

Value: my_result

Best regards.

Where my_result is the content of the corresponding variable.

How can I achieve the email I want managing the returns, the blank lines and the variable?

Rexam
  • 823
  • 4
  • 23
  • 42
  • Possible duplicate of [How do I send an HTML email?](https://stackoverflow.com/questions/5068827/how-do-i-send-an-html-email) – Niranjan Kumar Aug 20 '18 at 14:28
  • @NiranjanKumar Having blank lines etc in an email does not require sending HTML mail. – Mark Rotteveel Aug 20 '18 at 14:30
  • 1
    Your question is unclear and your title does not seem related to your actual question, and the solution to your question is basically the same when you wouldn't be sending an email, but instead wanted to print a string that looks like that: construct the text you want to send as an email, and then set that as text. There is nothing more to it (and if you think there is, then you may want to clarify your question further). – Mark Rotteveel Aug 20 '18 at 14:33
  • @MarkRotteveel I never used a MimeMultipart, so I thought there was more. Thanks for clarifying it. I will build the text with the StringBuilder and I will pass it to the messageBodyPart.setText(). – Rexam Aug 20 '18 at 14:39
  • 1
    @Rexam A `MimeMultiPart` is only relevant if you want to compose a message that consists of multiple parts (eg a body + attachments, or a plain text body + a html body (as alternatives), etc. What you seem to want is just a single part: the body text. – Mark Rotteveel Aug 20 '18 at 15:38

0 Answers0