standalone-full-ha.xml中的邮件服务配置。
<mail-session name="default" jndi-name="java:jboss/mail/Default" from="admin@mydomain.com">
<smtp-server outbound-socket-binding-ref="mail-smtp"
ssl="true"
username="sender@gmail.com"
password="password"/>
</mail-session>
<outbound-socket-binding name="mail-smtp">
<remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>
通过绑定到JNDI名称java:jboss/mail/Default
的服务注入邮件会话,并使用该邮件会话发送邮件。
@Resource(lookup = "java:jboss/mail/Default")
private Session mailSession; // javax.mail.Session
MimeMessage mimeMessage = new MimeMessage(mailSession);
Address[] to = new InternetAddress[]{new InternetAddress("recipient@gmail.com")};
mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, to);
mimeMessage.setSubject("Subject", "UTF-8");
mimeMessage.setSentDate(new Date());
mimeMessage.setText("Message", "UTF-8", "html");
Transport.send(mimeMessage);
现在,上面的XML配置中指定的发件人名称即from="admin@mydomain.com"
应该作为发件人发送,但服务器会继续发送{{1}指定的用户名。作为发件人。
在Java代码本身中执行以下操作无效,这在Java SE环境中都有用。
username="sender@gmail.com"
服务器不喜欢的后果。
使用WildFly 9.0.2 final。
更新:
服务器日志上的调试输出。
Address from = new InternetAddress("admin@mydomain.com");
mimeMessage.setFrom(from);
使用Java SE控制台应用程序发送邮件:
06:27:24,562 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
06:27:24,562 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: useEhlo true, useAuth true
06:27:24,562 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
06:27:25,296 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 220 smtp.gmail.com ESMTP g26sm26113121pfg.35 - gsmtp
06:27:25,296 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
06:27:25,296 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294))
06:27:25,296 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) EHLO om-f6b0ea3ad206
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-smtp.gmail.com at your service, [117.198.186.188]
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-SIZE 35882577
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-8BITMIME
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-ENHANCEDSTATUSCODES
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-PIPELINING
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250-CHUNKING
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250 SMTPUTF8
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "SIZE", arg "35882577"
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "8BITMIME", arg ""
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "PIPELINING", arg ""
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "CHUNKING", arg ""
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Found extension "SMTPUTF8", arg ""
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
06:27:25,625 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: AUTH LOGIN command trace suppressed
06:27:26,578 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: AUTH LOGIN succeeded
06:27:26,578 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: use8bit false
06:27:26,578 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) MAIL FROM:<sender@gmail.com>
06:27:26,875 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250 2.1.0 OK g26sm26113121pfg.35 - gsmtp
06:27:26,875 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) RCPT TO:<recipient@gmail.com>
06:27:27,156 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250 2.1.5 OK g26sm26113121pfg.35 - gsmtp
06:27:27,156 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: Verified Addresses
06:27:27,156 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: recipient@gmail.com
06:27:27,156 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DATA
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 354 Go ahead g26sm26113121pfg.35 - gsmtp
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Date: Sat, 30 Jan 2016 06:27:24 +0530 (IST)
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) To: recipient@gmail.com
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Message-ID: <17076900.1.1454115444562@gmail.com>
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Subject: Subject
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) MIME-Version: 1.0
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Content-Type: text/html; charset=UTF-8
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Content-Transfer-Encoding: 7bit
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294))
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) Message
06:27:27,515 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) .
06:27:28,437 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 250 2.0.0 OK 1454114106 g26sm26113121pfg.35 - gsmtp
06:27:28,437 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG SMTP: message successfully delivered to mail server
06:27:28,437 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) QUIT
06:27:28,718 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) 221 2.0.0 closing connection g26sm26113121pfg.35 - gsmtp
06:27:28,718 INFO [stdout] (Thread-21 (HornetQ-client-global-threads-7949294)) DEBUG: setDebug: JavaMail version 1.5.3
可以使用任意名称作为上面public class Mail {
public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
send("sender@gmail.com", "password", "recipient@gmail.com", "Subject", "Message", "Sender Name");
}
private static void send(String sender, String password, String recipient, String subject, String message, String senderName) throws MessagingException, UnsupportedEncodingException {
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.debug", "true");
props.put("mail.smtps.quitwait", "false");
Session session = Session.getInstance(props, null);
final MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(sender, senderName, "UTF-8"));
mimeMessage.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(recipient)});
mimeMessage.setSubject(subject, "UTF-8");
mimeMessage.setText(message, "UTF-8", "html");
mimeMessage.setSentDate(new Date());
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", sender, password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}
}
方法的最后一个参数中指定的发件人名称。
在打开发送的邮件时,发件人在邮件正文中显示为send()
。在前一种情况下,它显示为Sender Name <sender@gmail.com>
。
控制台终端设备上的调试输出:
sender@gmail.com
使用的发件人密码是在&#34; 2-Step Verification&#34;期间获得的应用程序专用密码。在DEBUG: JavaMail version 1.5.5
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,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
220 smtp.gmail.com ESMTP m87sm26277196pfi.47 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO om-f6b0ea3ad206
250-smtp.gmail.com at your service, [117.198.186.188]
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 XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<sender@gmail.com>
250 2.1.0 OK m87sm26277196pfi.47 - gsmtp
RCPT TO:<recipient@gmail.com>
250 2.1.5 OK m87sm26277196pfi.47 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: recipient@gmail.com
DATA
354 Go ahead m87sm26277196pfi.47 - gsmtp
Date: Sat, 30 Jan 2016 07:46:18 +0530 (IST)
From: Sender Name <sender@gmail.com>
To: recipient@gmail.com
Message-ID: <13329486.0.1454120181750@om-f6b0ea3ad206>
Subject: Subject
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
Message
.
250 2.0.0 OK 1454118752 m87sm26277196pfi.47 - gsmtp
DEBUG SMTP: message successfully delivered to mail server
QUIT
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
。
在这种情况下,API的版本差异可以忽略不计/可以安全地忽略。我已经尝试使用旧版本的java-mail API。前者使用版本1.5.3,后者使用版本1.5.5。