使用下面的代码,我无法发送电子邮件。没有抛出异常,但没有发送电子邮件。该代码是从运行在 Apache Tomcat 中的 servlet 中调用的。
该代码主要来自http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/上的在线示例。调试输出位于代码下方。
public static boolean sendEmail(String to, final String from, String subject, String emailMessage) {
final String username = from;
final String password = MyUtilities.getSystemPWD(from);
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", MyConfiguration.smtpServer);
props.put("mail.smtp.port", MyConfiguration.smtpPort);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setText(emailMessage);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException me) {
MyLogger.log("MyUtilities.sendEmail: Messaging error", me);
Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", me);
System.out.println("MyUtilities.sendEmail: Messaging error");
return false;
} catch (Exception ex) {
MyLogger.log("MyUtilities.sendEmail: Messaging error", ex);
Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", ex);
return false;
}
return true;
}
调试输出
DEBUG: setDebug: JavaMail version 1.4.5
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 "mail.mysite.com", port 587, isSSL false
220 smtp.mysite.net InterWorx-CP SMTP Server ESMTP
DEBUG SMTP: connected to host "mail.mysite.com", port: 587
EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-STARTTLS
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
220 ready for tls
EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<notifications@myaddress.com>
250 ok
RCPT TO:<xxxxxxxxxx@hotmail.com>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: xxxxxxx@hotmail.com
DATA
354 go ahead
From: notifications@myaddress.com
To: xxxxxx@hotmail.com
Message-ID: <256511087.2.1381941482464.JavaMail.xxxxx@xxxx-iMac.local>
Subject:
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
<html></html>
.
250 ok 1381941483 qp 29906
QUIT
221 smtp.mysite.net InterWorx-CP SMTP Server
Done