【发布时间】:2014-08-09 22:07:20
【问题描述】:
我正在开发一个监控程序,它会定期发送电子邮件。 一切都很顺利,直到我试图把它变成一个可运行的 jar。 邮件部分在 Eclipse 中运行良好,我可以发送电子邮件。但是一旦我把它变成一个可运行的 jar,程序就永远不会发送电子邮件。
我做错了什么?
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);
}
}
这是从 Eclipse 运行时的 Eclipse 控制台。 注意:最后关闭连接。 此代码发送电子邮件。
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
这是来自 Win7 cmd 使用 > java -jar EmailTest.jar 注意:程序永远不会退出。没有关闭连接。 此 jar 不发送电子邮件。
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
【问题讨论】:
-
帮自己一个忙 - 从您的帖子中删除电子邮件地址-密码组合。
-
...并尽快更改密码
-
谢谢你们。很分心没有注意到。
标签: java eclipse email executable-jar