I am developing a monitoring program, which sends periodic emails. Every thing went fine until I tried to make it a runnable jar. The mailing part is working fine in eclipse and I am able to send email. But as soon as I make it a runnable jar the program never sends email.
What am I doing wrong?
package mailing;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class TestEmail
{
void addRecipients(Message.RecipientType type, Address[] addresses)throws MessagingException
{
}
public static void main(String [] args) throws Exception
{
final int port = 465;
final String host = "smtp.gmail.com";
final String from = "*****@gmail.com";
final String to = "********@gmail.com";
boolean auth = true;
final String username = "*******@gmail.com";
final String password = "**********";
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
Authenticator authenticator = null;
props.put("mail.smtp.auth", true);
authenticator = new Authenticator() {
PasswordAuthentication pa = new PasswordAuthentication(username, password);
@Override
public PasswordAuthentication getPasswordAuthentication() {
return pa;
}
};
Session session = Session.getInstance(props, authenticator);
session.setDebug(debug);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to.toString())};
message.setRecipients(Message.RecipientType.TO, address);
String subject="- Job Alert -";
message.setSubject(subject);
message.setSentDate(new Date());
String body="Hello you got a new mail";
message.setText(body);
Transport.send(message);
}
}
This is eclipse console when running from eclipse. Notice : closing connection in the end. This code sends email.
DEBUG: setDebug: JavaMail version 1.4.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
220 mx.google.com ESMTP fk4sm25070066pab.23 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO jason-DesktopPC1
250-mx.google.com at your service, [49.204.224.235]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250 CHUNKING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
AUTH LOGIN
334 VXNlcm5hbWU6
Y2hlcnkwMTJAZ21haWwuY29t
334 UGFzc3dvcmQ6
VGFuaXRhbmkwMTI=
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<chery012@gmail.com>
250 2.1.0 OK fk4sm25070066pab.23 - gsmtp
RCPT TO:<avinandan012@gmail.com>
250 2.1.5 OK fk4sm25070066pab.23 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: avinandan012@gmail.com
DATA
354 Go ahead fk4sm25070066pab.23 - gsmtp
Date: Thu, 19 Jun 2014 16:17:57 +0530 (IST)
From: chery012@gmail.com
To: avinandan012@gmail.com
Message-ID: <399262.0.1403174877546.JavaMail.example@gmail.com>
Subject: - Job Alert -
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hello you got a new mail
.
250 2.0.0 OK 1403174885 fk4sm25070066pab.23 - gsmtp
QUIT
221 2.0.0 closing connection fk4sm25070066pab.23 - gsmtp
This is from Win7 cmd using > java -jar EmailTest.jar Notice: The programme never quits. No closing connection. This jar does not sends email.
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
220 mx.google.com ESMTP ih6sm8024528pbc.22 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO jason-DesktopPC1
250-mx.google.com at your service, [49.204.224.235]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250 CHUNKING
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5
AUTH LOGIN
334 VXNlcm5hbWU6
Y2hlcnkwMTJAZ21haWwuY29t
334 UGFzc3dvcmQ6
VGFuaXRhbmkwMTI=
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<chery012@gmail.com>
250 2.1.0 OK ih6sm8024528pbc.22 - gsmtp
RCPT TO:<avinandan012@gmail.com>
250 2.1.5 OK ih6sm8024528pbc.22 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: avinandan012@gmail.com
DATA
354 Go ahead ih6sm8024528pbc.22 - gsmtp
Date: Thu, 19 Jun 2014 16:30:51 +0530 (IST)
From: chery012@gmail.com
To: avinandan012@gmail.com
Message-ID: <32535959.01403175651787.JavaMail.example@gmail.com>
Subject: - Job Alert -
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hello you got a new mailQUIT