Java邮件runnable jar没有运行

时间:2014-06-19 11:24:36

标签: java eclipse email executable-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使用&gt; 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

1 个答案:

答案 0 :(得分:0)

我不确定这是否有效,只需尝试代码:

public boolean sendMail(final String senderEmail, final String password, String recipientEmail, String ccEmail, String bccEmail) {

    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "25");

    Session session = Session.getInstance(props, null);

    try {

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(senderEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
        if (ccEmail.length() > 0) {
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail));
        }
        if(bccEmail.length() > 0){
            message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bccEmail));
        }
        message.setSubject(this.subject);
        message.setSentDate(new Date());
        message.setText(this.textMessage, "utf-8");

        SMTPTransport t = (SMTPTransport) session.getTransport("smtps");

        t.connect("smtp.gmail.com", senderEmail, password);
        t.sendMessage(message, message.getAllRecipients());
        t.close();

        System.out.println("Message sent successfully to '" + recipientEmail + "; " + ccEmail + "'.");

        return true;
    } catch (MessagingException e) {
        System.out.println("Message not sent to '" + recipientEmail + "; " + ccEmail + "'.");
        e.printStackTrace();
        return false;
    }
}