= Configuring JavaMail
:index-group: Configuration
:jbake-date: 2018-12-05
:jbake-type: page
:jbake-status: published

= Declaring a JavaMail Resource

The basics are that any properties listed in the element are given
directly to the javamail provider via
jakarta.mail.Session.getDefaultInstance(Properties props).

Here might be some example properties.

[source,xml]
----
<Resource id="SuperbizMail" type="jakarta.mail.Session">
   mail.smtp.host=mail.superbiz.org
   mail.smtp.port=25
   mail.transport.protocol=smtp
   mail.smtp.auth=true
   mail.smtp.user=someuser
   password=mypassword
</Resource>
----

You can create as many entries like this as you wish, they just have to
have a unique 'id'.

Careful not to add whitespace at the end of your property values. A
java.util.Properties object will leave those in the property values, and
they will be passed to the JavaMail provider with the whitespace on the
end which may cause issues if the provider does not actively trim the
values before attempting to use them.

== Overriding

If you wanted to do a System property or InitialContext property
override of the above example mail session, you could do so like this:

[source,bash]
----
java ... -DSuperbizMail.mail.smtp.host=localhost
----

== IMAP SASL XOAUTH2 mechanism configuration

=== Code example for gmail XOAUTH2

[source,bash]
----
Properties props = new Properties();
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.debug", String.valueOf(true));
props.setProperty("mail.debug.auth", String.valueOf(true));
props.setProperty("mail.imap.sasl.enable", String.valueOf(true));
props.setProperty("mail.imap.sasl.mechanisms", "XOAUTH2");
props.setProperty("mail.imap.ssl.enable", "true");
props.setProperty("mail.imap.starttls.enable", "true");
props.setProperty("mail.imap.starttls.required", "true");
props.setProperty("mail.imap.auth.login.disable", "true");
props.setProperty("mail.imap.auth.plain.disable", "true");

Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", "<username>@gmail.com", "<YourAccesToken>");
----

=== Using tomee.xml

`tomee.xml` example:

[source,bash]
----
<Resource id="ImapSession" type="jakarta.mail.Session">
mail.imap.ssl.enable=true
mail.imap.starttls.enable=true
mail.imap.starttls.required=true
mail.imap.sasl.enable=true
mail.imap.sasl.mechanisms=XOAUTH2
mail.host=imap.gmail.com
useDefault=false
accessToken=<YourAccesToken>
</Resource>
----

Usage example:

[source,bash]
----
private final String from = "<username>@gmail.com";

@Resource(name = "ImapSession")
Session imapSession;

public class EmailService {

 private void readTheLast5SentEmails(){
    final Store store = imapSession.getStore("imap");
    store.connect(from, imapSession.getProperty("accessToken"));
    final Folder sentFolder = store.getFolder("[Gmail]/Sent Mail");
    sentFolder.open(Folder.READ_ONLY);
    final int messageCount = sentFolder.getMessageCount();
    final Message[] messages = sentFolder.getMessages(messageCount - 5, messageCount);
 }

}
----