要在运行Java邮件程序时要求Java邮件打印出调试消息,可以添加mail.debug并将其值设置true为用于创建会话对象的属性。
另一种方法是使用session.setDebug()方法设置会话的调试设置并true作为参数传递。这还将打开Java邮件程序中的调试消息。
package org.nhooo.example.mail; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; public class MailDebugDemo { public static void main(String[] args) { MailDebugDemo.sendMail("nhooo@gmail.com", "nhooo@gmail.com", "Debug Demo", "Mail Debug Demo"); } private static void sendMail(String mailFrom, String mailTo, String mailSubject, String mailText) { Properties props = new Properties() {{ put("mail.smtp.auth", "true"); put("mail.smtp.host", "smtp.gmail.com"); put("mail.smtp.port", "587"); put("mail.smtp.starttls.enable", "true"); put("mail.debug", "true"); }}; //创建一个邮件会话。我们需要提供用户名和 // GMail验证的密码。 Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("nhooo", "********"); } }); try { // 创建电子邮件 Message message = new MimeMessage(session); message.setSentDate(new Date()); message.setFrom(new InternetAddress(mailFrom)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); message.setSubject(mailSubject); message.setText(mailText); // 发送消息 Transport.send(message, "nhooo", "********"); } catch (MessagingException e) { e.printStackTrace(); } } }
这是使用Gmail SMTP服务器发送电子邮件时JavaMail API生成的调试信息的示例。
DEBUG: JavaMail version 1.5.6 DEBUG: successfully loaded resource: /META-INF/javamail.default.providers DEBUG: Tables of loaded providers DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]} DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]} DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false 220 smtp.gmail.com ESMTP e64sm51975202pfl.49 - gsmtp DEBUG SMTP: connected to host "smtp.gmail.com", port: 587 EHLO pluto 250-smtp.gmail.com at your service, [180.252.60.172] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "STARTTLS", arg "" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" STARTTLS 220 2.0.0 Ready to start TLS EHLO pluto 250-smtp.gmail.com at your service, [180.252.60.172] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 DEBUG SMTP: Found extension "SIZE", arg "35882577" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN succeeded DEBUG SMTP: use8bit false MAIL FROM:<nhooo@gmail.com> 250 2.1.0 OK e64sm51975202pfl.49 - gsmtp RCPT TO:<nhooo@gmail.com> 250 2.1.5 OK e64sm51975202pfl.49 - gsmtp DEBUG SMTP: Verified Addresses DEBUG SMTP: nhooo@gmail.com DATA 354 Go ahead e64sm51975202pfl.49 - gsmtp Date: Mon, 5 Jun 2017 12:12:54 +0800 (WITA) From: nhooo@gmail.com To: nhooo@gmail.com Message-ID: <1368884364.0.1496635974662@pluto> Subject: Debug Demo MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Mail Debug Demo . 250 2.0.0 OK 1496635979 e64sm51975202pfl.49 - gsmtp QUIT 221 2.0.0 closing connection e64sm51975202pfl.49 - gsmtp
Maven依赖
<!-- http://repo1.maven.org/maven2/javax/mail/javax.mail-api/1.5.6/javax.mail-api-1.5.6.jar --> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.6</version> </dependency> <!-- http://repo1.maven.org/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>