bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add convenience methods for replying to and forwarding emails

lacivert opened this issue · comments

commented

Is there a feature like reply for a specific Message object?

I have an IMAP connection and I can read mails, so I have a Message object. How can I reply to this message by using simple-java-mail?

Hello @lacivert, you can do a couple of things. but really all you need is the subject and replyToAddress, or when missing the fromAddress.

Either you get it directly from the Message object (I'm assuming you mean MimeMessage) or you can convert the MimeMessage to an Email object and then easily get the required infomation:

Email fromEmail = EmailConverter.mimeMessageToEmail(yourMimeMessage);
Email email = new EmailBuilder()
    .from("Your Name", "your.name@domain.com")
    .to(fromEmail.getFromRecipient().getName(), fromEmail.getFromRecipient().getAddress())
    .subject("Re: " + fromEmail.getSubject())
    .text("...") // .textHTML("...")
    .build();

However, it seems like a nice convenience method to add asReplyTo(Email) and asForwardOf(Email). I'll add it to the list, but for now you can use the above example.

commented

Thank you very much. For now, as you stated, I use such a solution. But the "original message" in reply doesn't look very well. I don't know if there is a standard solution for it but my replies doesn't look collapsed when I look from Gmail. Do you have any idea about it?

I guess this has something to do with all the headers used in MimeMessage.reply(). Still looking into it.

I've implemented replyTo functionality in the EmailBuilder class, but there's a catch. Currently I don't include a quote of the replied-to email's body. Perhaps that should be an optional flag, but as it stands the body entirely depends on the user. I'm note sure it's even possible, since Simple Java Mail cannot determine the HTML to be used (this is highly user-specific).

Email = new EmailBuilder()
      .asReplyTo(emailOrMessage)
      .from("name@domain.com")
      .textHTML("your body with or without quote of the email")
      .build();

Now forwarding is left, which is actually a much simpler case.

Turns out forwarding is a much more difficult case, because as opposed to replying, forwarding actually includes the original message in the MimeMultiPart structure.

However, it's working now, both replying and forwarding! Will be in the 5.0.0 release coming up soon.

Released as 5.0.0.rc1-SNAPSHOT. Add OSS' snapshots repo to find it in Maven.

EmailBuilder.startingBlank()
EmailBuilder.replyingTo(email)
EmailBuilder.forwarding(email)
EmailBuilder.copying(email)