我正在使用Java Mail API以pdf作为附件发送邮件。 请参阅下面的代码
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
System.out.println(toUserEmails);
for(int i = 0 ; i < toUserEmails.length() ; i++){
String email = toUserEmails.optString(i);
message.addRecipients(
Message.RecipientType.TO,InternetAddress.parse(email));
}
for(int i = 0 ; i < ccUserEmails.length() ; i++){
String email = ccUserEmails.optString(i);
message.addRecipient(Message.RecipientType.CC,new
InternetAddress(email));
}
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
if(!attachment.equals("") && attachment != null){
System.out.println("attachment base64 is " + attachment);
byte[] attach = Base64.decodeBase64(new
String(attachment).getBytes("UTF-8"));
JSONObject mimeTypes = new JSONObject().put("pdf",
"application/pdf");
System.out.println(attachmentName);
String ext = attachmentName.split("\\.")[1];
DataSource source = new
ByteArrayDataSource(attach,mimeTypes.getString(ext));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);
Transport.send(message);
它在我的本地机器(窗口8)中工作正常。 但由于某些原因,当我在ubuntu上的服务器上部署此代码时,邮件将被发送,而不是任何正文,甚至是文本。和一个名为&#34; noname&#34;的无效文件正在接近。
我尝试重新启动服务器,更改jar文件版本并稍微更改了代码但没有任何效果。
我无法在stackoverflow上找到任何内容。 我正在IBM Websphere Application服务器Liberty上部署war文件。 任何人都可以帮助我吗?
成功附加pdf的调试输出
DEBUG: JavaMail version 1.5.0
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: setDebug: JavaMail version 1.5.0
asdfsd.pdf
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 465, isSSL true
220 smtp.gmail.com ESMTP r13sm21216626pfl.157 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
EHLO Yash-PC
250-smtp.gmail.com at your service, [103.239.146.50]
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: STARTTLS requested but already using SSL
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:<stre****@gmail.com>
250 2.1.0 OK r13sm21216626pfl.157 - gsmtp
RCPT TO:<yashpatel****@gmail.com>
250 2.1.5 OK r13sm21216626pfl.157 - gsmtp
DEBUG SMTP: Verified Addresses
DEBUG SMTP: yashpatel****@gmail.com
DATA
354 Go ahead r13sm21216626pfl.157 - gsmtp
From: stre*****@gmail.com
To: yashpatel****@gmail.com
Message-ID: <604468520.10.1512892148895.JavaMail.admin@smtp.gmail.com>
Subject: Important Notice
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_9_1517326996.1512892148882"
------=_Part_9_1517326996.1512892148882
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
This is a notice
------=_Part_9_1517326996.1512892148882
Content-Type: application/pdf; name=asdfsd.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=asdfsd.pdf
<REALLY LONG BASE 64>
------=_Part_9_1517326996.1512892148882--
.
250 2.0.0 OK 1512892145 r13sm21216626pfl.157 - gsmtp
QUIT
221 2.0.0 closing connection r13sm21216626pfl.157 - gsmtp
Send
无法附加pdf或正文的调试输出
DEBUG: JavaMail version 1.6.0
O DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
O DEBUG: Tables of loaded providers
O 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]}
O 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]}
O DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
O ["yashpatel*****@gmail.com"]
O O asdfsd.pdf
O DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
O DEBUG SMTP: need username and password for authentication
O DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=root, password=<null>
O DEBUG SMTP: useEhlo true, useAuth true
O DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
O 220 smtp.gmail.com ESMTP y2sm5395957otg.51 - gsmtp
O DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
O EHLO mfppreprod.streebo.com
O 250-smtp.gmail.com at your service, [198.176.30.250]
O 250-SIZE 35882577
O 250-8BITMIME
O 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
O 250-ENHANCEDSTATUSCODES
O 250-PIPELINING
O 250-CHUNKING
O 250 SMTPUTF8
O DEBUG SMTP: Found extension "SIZE", arg "35882577"
O DEBUG SMTP: Found extension "8BITMIME", arg ""
O DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
O DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
O DEBUG SMTP: Found extension "PIPELINING", arg ""
O DEBUG SMTP: Found extension "CHUNKING", arg ""
O DEBUG SMTP: Found extension "SMTPUTF8", arg ""
O DEBUG SMTP: STARTTLS requested but already using SSL
O DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, user=stre******@gmail.com, password=<non-null>
O DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
O DEBUG SMTP: Using mechanism LOGIN
O DEBUG SMTP: AUTH LOGIN command trace suppressed
O DEBUG SMTP: AUTH LOGIN succeeded
O DEBUG SMTP: use8bit false
O MAIL FROM:<stre******@gmail.com>
O 250 2.1.0 OK y2sm5395957otg.51 - gsmtp
O RCPT TO:<yashpatel*****@gmail.com>
O 250 2.1.5 OK y2sm5395957otg.51 - gsmtp
O DEBUG SMTP: Verified Addresses
O DEBUG SMTP: yashpatel*****@gmail.com
O DATA
O 354 Go ahead y2sm5395957otg.51 - gsmtp
O Date: Sun, 10 Dec 2017 13:07:59 +0530 (IST)
O From: stre******@gmail.com
O To: yashpatel*****@gmail.com
O Message-ID: <1399918662.3.1512891479245@smtp.gmail.com>
O Subject: Important Notice
O MIME-Version: 1.0
O Content-Type: multipart/mixed;
O boundary="----=_Part_2_376671468.1512891479133"
O .
O 250 2.0.0 OK 1512891481 y2sm5395957otg.51 - gsmtp
O DEBUG SMTP: message successfully delivered to mail server
O QUIT
O 221 2.0.0 closing connection y2sm5395957otg.51 - gsmtp
O Send