Reputation: 641
I need to send a mail using SMTP of Gmail, and javax.mail api. The same code I'm using, runs successfully in Android, If I take it to a Java Application or tries to use it in a Java Web Application it starts making troubles. I spent time trying to understand what is the difference but no way! My code is as following:
public class GMailSender extends Authenticator
{
private final String mailhost;
private final String password;
private final Session session;
private final String user;
public GMailSender(String username, String password)
{
this.mailhost = "smtp.gmail.com";
this.user = username;
this.password = password;
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", mailhost);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.debug", "true");
properties.setProperty("mail.smtp.quitwait", "false");
System.out.println("Creating session ...");
session = Session.getInstance(properties, this);
System.out.println("Session createed ...");
}
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
System.out.println("Authintecation ...");
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String dataHandler, String senderAddress, String recepeintAddress)
throws Exception
{
MimeMessage mimemessage;
mimemessage = new MimeMessage(session);
DataHandler datahandler = new DataHandler(new ByteArrayDataSource(dataHandler.getBytes(), "text/plain"));
mimemessage.setSender(new InternetAddress(senderAddress));
mimemessage.setSubject(subject);
mimemessage.setDataHandler(datahandler);
mimemessage.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recepeintAddress));
System.out.println("Sending ...");
Transport transport = session.getTransport("smtp");
transport.send(mimemessage);
System.out.println("Sent!");
}
static {
Security.addProvider(new JSSEProvider());
}
public static void main(String[] args){
System.out.println("Starting email ...");
GMailSender sender = new GMailSender("[email protected]", "my password");
try {
sender.sendMail("Test", "alot of data", "[email protected]", "[email protected]");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public final class JSSEProvider extends Provider {
private static final long serialVersionUID = 1L;
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController
.doPrivileged(new java.security.PrivilegedAction<Void>() {
@Override
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
When I run my code I got the following:
> Starting email ...
> Creating session ...
> Session createed ...
> Sending ...
> Authintecation ...
Then it takes around 10 min to return with the following:
> javax.mail.MessagingException: Could not connect to SMTP host:
> smtp.gmail.com, port: 465, response: -1
> at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
> at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
> at javax.mail.Service.connect(Service.java:310)
> at javax.mail.Service.connect(Service.java:169)
> at javax.mail.Service.connect(Service.java:118)
> at javax.mail.Transport.send0(Transport.java:188)
> at javax.mail.Transport.send(Transport.java:118)
> at com.srycrm.mail.GMailSender.sendMail(GMailSender.java:66)
> at org.apache.jsp.send_jsp._jspService(send_jsp.java:85)
> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Anyone can help me please!! Thank you.
Upvotes: 0
Views: 431
Reputation: 29971
There's a bunch of errors in your code. Start here to fix the most common mistakes.
After that, see the JavaMail FAQ for connection debugging tips. Post the debug output here if you can't figure it out.
Upvotes: 0
Reputation: 641
Great! Thanks for all the people that tried to help.
The Solution is a bit strange! I just downgraded my JRE and JDK to 1.6 and this solved the problem! It might be something with the Java 1.7 enviroment.
Anyway, Thanks for all you are like always awesome :)
Upvotes: 1
Reputation: 3682
Ok you are using port 465, so enable ssl mail.smtp.ssl.enable
to true
:
properties.put("mail.smtp.ssl.enable", "true");
if it doesn;t work , then have properties.put("mail.smtp.starttls.enable", "true");
and change port to 587
and see if it helps.
Upvotes: 1