I have an application I wrote in JavaSpring that uses the JavaMail API to send an email. I would like it to use my Office 365 Exchange service instead of my localhost sendmail service on linux. This will avoid any spoofing issues downstream. It is unclear to me, however, how to set up this handshake. I need advice.
Here is what I know so far:
In my application-context file I create a bean for JavaMail:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="localhost"/>
<property name="port" value="25"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">false</prop>
<!--
<prop key="mail.debug">true</prop>
-->
</props>
</property>
</bean>
I will change to
I don't know what else I need to configure, though:
What must I configure in exchange to receive these requests? (i.e. How do I handshake with my O365 Exchange service?) I saw something about Mail Flow in O365 Exchange and Connections, but I don't see any means of setting up authentications for such services. Do I need to create an new special account with username and password and use that via the application instead?
Do I need to setup anything on the linux server on which my java application is running? (hostname, etc.)
Any experts or online documentation I can consult would be extremely useful to me.
Sincerely,
Stephen.
I found the answer:
JavaMail configuration:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.office365.com"/>
<property name="port" value="587"/>
<property name="username" value="foo@bar.com"/>
<property name="password" value="****"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">false</prop>
</props>
</property>
</bean>
NOTES:
Office365 has a special host for SMTP: smtp.office365.com
Use port 587 for security purposes.
One must register a special O365 email account through which to send the mail. (ex. foo@foo.bar). The message.setFrom() method must match the account used (ex. foo@foo.bar) or it will not be sent via that account by Exchange (an error is thrown).
TLS must be set to TRUE
Supporting information:
https://howtodoinjava.com/spring/spring-core/send-email-with-spring-javamailsenderimpl-example/