Search code examples
springjakarta-mailexchange-server-2010

How to configure JavaMail to use Office 365 Exchange Service to send mail


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.


Solution

  • 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="[email protected]"/>
            <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. [email protected]). The message.setFrom() method must match the account used (ex. [email protected]) or it will not be sent via that account by Exchange (an error is thrown).

    TLS must be set to TRUE

    Supporting information:

    https://support.office.com/en-us/article/How-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4?ui=en-US&rs=en-US&ad=US

    https://howtodoinjava.com/spring/spring-core/send-email-with-spring-javamailsenderimpl-example/