Afjali
Afjali

Reputation: 31

Issue in sending a mail to gmail account using spring mail

I was facing a issue while sending a mail to gmail account using spring mail. I referred most of the posts from stackoverflow and tried those. But no luck. Still am stuck with it. Please suggest what is still missing from my code.

Windows -8: Java 7

EmailServiceImpl.java:

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import com.test.mail.data.EmailData;

public class EmailServiceImpl {                                    
private JavaMailSender mailSender;

public JavaMailSender getMailSender() {
return mailSender;
}

public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
    public void sendMail(EmailData emailData) {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);
    try {
        helper.setFrom(emailData.getFrom());
        helper.setTo(emailData.getTo());
        helper.setSubject(emailData.getSubject());
        helper.setText(emailData.getBody());
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    mailSender.send(message);
}
}

application-context.xml

            <bean id="emailSvcImpl"
                            class="com.test.mail.services.impl.EmailServiceImpl">
                            <property name="mailSender" ref="mailSender" />
                            </bean>


            <!-- via TLS -->

            <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
                            <property name="host" value="smtp.gmail.com" />
                            <!-- <property name="port" value="25" /> -->  <!-- Also checked with port number : 25 -->
                            <property name="port" value="587" />
                            <property name="protocol" value="smtp" />
                            <property name="username" value="[email protected]" />
                            <property name="password" value="fromaccountpwd" />
                            <property name="javaMailProperties">
                                            <props>
                                                            <prop key="mail.smtp.auth">true</prop>
                                                            <prop key="mail.smtp.starttls.enable">true</prop>
                                                            <prop key="mail.smtp.debug">true</prop>
                                                            <prop key="mail.smtp.timeout">8500</prop>
                                            </props>
                            </property>
            </bean>


            <!-- Also Tried  to send gmail via SSL -->
            <!-- 
            <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
                            <property name="host" value="smtp.gmail.com" />
                            <property name="port" value="465" />
                            <property name="protocol" value="smtps" />
                            <property name="username" value="[email protected]" />
                            <property name="password" value="fromaccountpwd" />
                <property name="javaMailProperties">
                  <props>
                <prop key="mail.smtps.auth">true</prop>
                 <prop key="mail.smtps.starttls.enable">true</prop>
                   <prop key="mail.smtps.debug">true</prop>
                      <prop key="mail.smtps.timeout">8500</prop>
                        </props>
                    </property>
            </bean>  -->

Maven dependencies:

            <dependency>
                       <groupId>org.springframework</groupId>
                       <artifactId>spring-context-support</artifactId>
                       <version>3.2.11.RELEASE</version>
             </dependency>

            <dependency>
                 <groupId>com.sun.mail</groupId>
                 <artifactId>javax.mail</artifactId>
                 <version>1.5.2</version>
            </dependency>

cmd prompt: used'ping smtp.gmail.com' command to ping , am able to get the reply and no loss of data.

Exception :

   org.apache.cxf.interceptor.Fault: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host,   port: smtp.gmail.com, 465; timeout -1;
nested exception is:
           java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
           java.net.ConnectException: Connection refused: connect

Tried sending mail with SSL and TLS , but same exception with different port numbers

Upvotes: 3

Views: 4757

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29971

It looks like there's a firewall on your network preventing you from connecting. You may need to configure JavaMail to work through your proxy server.

FYI, connection debugging tips are also in the JavaMail FAQ.

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12401

Google has changed the policies, you need to allow your account to be accessed by applications.

I came across an issue yesterday and received this email via google.com

You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.

once you are done with is all, you can verify the properties, this is what I have used and it is working perfectly on my end

 Properties props = new Properties();
 props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
 props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
 props.put("mail.smtp.socketFactory.class",
 "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
 props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
 props.put("mail.smtp.port", "465"); //SMTP Port

I hope you are using the correct values.

Upvotes: 4

Related Questions