tags:

views:

1061

answers:

5

I am trying to find the correct properties to use to connect to the Gmail SMTP sever using the JavaMailSenderImpl class.

Let me first say that I have tried the approach found here. This worked fine. But when I tried the configuration below that post with the exact same authentication information I received a javax.mail.AuthenticationFailedException.

My currently configuration looks like this.

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="username" value="[email protected]" />
    <property name="password" value="XXX" />
    <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.host">smtp.gmail.com</prop>
        <prop key="mail.smtp.port">587</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
    </property>
</bean>

Why am I still getting this javax.mail.AuthenticationFailedException if I know that my credentials are correct.

Update

Here is my updated code based on the answers below. I am still receiving the same exception.

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="username" value="[email protected]" />
    <property name="password" value="XXX" />
    <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.from">[email protected]</prop>
        <prop key="mail.smtp.user">[email protected]</prop>
        <prop key="mail.smtp.password">XXX</prop>
        <prop key="mail.smtp.host">smtp.gmail.com</prop>
        <prop key="mail.smtp.port">587</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
    </property>
</bean>
A: 

This doesn't seem significantly different, but perhaps try:

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="username" value="[email protected]" />
    <property name="password" value="XXX" />
    <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.user" value="[email protected]" />
        <prop key="mail.smtp.password" value="XXX" />
        <prop key="mail.smtp.host">smtp.gmail.com</prop>
        <prop key="mail.smtp.port">587</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
    </property>
</bean>
jsight
I have tried that before with no luck. I also tried the mail.smtp.passwd property as well just for kicks.
Andrew Carlson
A: 

You should specify your "from" address, either as <prop key="mail.smtp.from">[email protected]</prop>, or when creating a message.

axtavt
I was setting it when creating the message but I have also added the property in my updated post.
Andrew Carlson
A: 

I have now resolved this issue.

The problem was with the actual existing Java code I was using.

The solution can be found here.

Andrew Carlson
A: 

This worked for me:

        <property name="host"><value>smtp.gmail.com</value></property>
        <property name="port"><value>587</value></property>
        <property name="protocol"><value>smtp</value></property>
        <property name="username"><value>${mail.username}</value></property>
        <property name="password"><value>${mail.password}</value></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.quitwait">false</prop>
            </props>
        </property>

The real trick for me turned out to be that the "protocol" value has to be "smtp" (not "smtps").

Ken Burcham
A: 

The only property needed for GMail is

<prop key="mail.smtp.starttls.enable">true</prop>

Timo

timomeinen