Aarkan
Aarkan

Reputation: 4109

Using Java to send emails on gmail account

I know this question is asked before but I think it was 2-3 years before and things have changed since then. None of the code samples is working now. I am trying the following code in Java 1.6:

Properties props = new Properties();
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("uname","password");
    }
});
session.setDebug(true);
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress("[email protected]");
MimeMessage message = new MimeMessage(session);
message.setSender(addressFrom);

for (int j = 0; j < 20; j++) {
    message.setSubject("Testing javamail plain"+Math.random());
    message.setContent("This is a test", "text/plain");
    String sendTo [] = {"[email protected]"};
    if (sendTo != null) {
        InternetAddress[] addressTo = new InternetAddress[sendTo.length];
        for (int i = 0; i < sendTo.length; i++) {
            addressTo[i] = new InternetAddress(sendTo[i]);
        }
        message.setRecipients(Message.RecipientType.TO, addressTo);
    }
    transport.connect();
    Transport.send(message);
    transport.close();
    System.out.println("DONE");
}

and it throws this exception:

org.apache.cxf.interceptor.Fault: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:155)
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:121)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:164)
org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:91)
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
java.util.concurrent.FutureTask.run(FutureTask.java:138)
org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:106)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:206)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:218)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:161)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:114)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:184)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:112)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:163)

I am using tomcat as webserver and cxf framefork for webservices. Any help is much appreciated.

Upvotes: 2

Views: 3889

Answers (3)

Ritesh Mengji
Ritesh Mengji

Reputation: 6190

I assume that you have java.mail jar file in your class path and cxf supporting jar files. Actually CXF makes use of Apache Geronimo Javamail/activation implementations, so now you will have two different versions of Sun's Javamail libraries in your application classpath, When your application tries to send an email, Java gets confused as to which version of the library it should be using, and falls over. So the solution might be either removing the javamail jar file or to exclude the geronimo javamail in the pom file like :

<exclusions>          
     <exclusion>               
         <groupId>org.apache.geronimo.specs</groupId>  
         <artifactId>geronimo-javamail_1.4_spec</artifactId>    
     </exclusion>
     <exclusion>  
          <groupId>org.apache.geronimo.specs</groupId>   
          <artifactId>geronimo-activation_1.1_spec</artifactId> 
     </exclusion>  

Hope this help you.

Upvotes: 6

user720618
user720618

Reputation:

This might help you: Add the following in the pom.xml if you are using Maven

<dependency>
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-rt-core</artifactId> 
     <version>${cxf.version}</version>   
     <exclusions>          
         <exclusion>               
             <groupId>org.apache.geronimo.specs</groupId>  
             <artifactId>geronimo-javamail_1.4_spec</artifactId>    
         </exclusion>
         <exclusion>  
              <groupId>org.apache.geronimo.specs</groupId>   
              <artifactId>geronimo-activation_1.1_spec</artifactId> 
         </exclusion>  
</exclusions>                
</dependency>     
<dependency>    
     <groupId>org.apache.cxf</groupId> 
     <artifactId>cxf-rt-frontend-jaxws</artifactId>     
     <version>${cxf.version}</version>   
</dependency>             
<dependency>   
     <groupId>org.apache.cxf</groupId>  
     <artifactId>cxf-rt-databinding-jaxb</artifactId>    
     <version>${cxf.version}</version>  
     <exclusions>      
          <exclusion>  
               <groupId>org.apache.geronimo.specs</groupId>
               <artifactId>geronimo-javamail_1.4_spec</artifactId>        
          </exclusion>    
          <exclusion>    
               <groupId>org.apache.geronimo.specs</groupId>   
               <artifactId>geronimo-activation_1.1_spec</artifactId>    
          </exclusion>       
     </exclusions>         
</dependency>

Then, add the Sun javamail/activation implementations instead :

<dependency>      
     <groupId>javax.mail</groupId>   
     <artifactId>mail</artifactId>   
     <version>1.4</version>  
</dependency>

Upvotes: 3

Marcelo
Marcelo

Reputation: 11308

You need to fulfill the javax.mail dependency. If you are using Maven, add the following to your pom.xml file:

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.4</version>
</dependency>

Upvotes: 0

Related Questions