Java - SimpleMimeMessage does not set subject
I use Apache Commons Mail and recognised that the email subject (and other settings like character encoding) are not used at开发者_Python百科 all:
props.put("mail.smtp.host", "localhost");
Session s = Session.getInstance(props, null);
s.setDebug(true);
MimeMessage message = new MimeMessage(s);
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
message.addHeader("Content-Transfer-Encoding", "quoted-printable");
message.setFrom(new InternetAddress("me@home.com"));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("you@abroad.com"));
String subj = MimeUtility.encodeText("");
message.setSubject("my subject with specials äöü");
message.setText("Some text with special äöü");
Transport.send(message);
The debbugging output shows:
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
220 mydomain ESMTP Postfix
DEBUG SMTP: connected to host "localhost", port: 25
EHLO MyComputer
250-PIPELINING
250-SIZE 10485760
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "10485760"
DEBUG SMTP: Found extension "VRFY", arg ""
DEBUG SMTP: Found extension "ETRN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: use8bit false
MAIL FROM:<me@home.com>
250 2.1.0 Ok
RCPT TO:<you@abroad.com>
250 2.1.5 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: you@abroad.com
DATA
354 End data with <CR><LF>.<CR><LF>
Some text with specials =C3=A4=C3=B6=C3=BC
.
250 2.0.0 Ok: queued as 9F623345F99
QUIT
221 2.0.0 Bye
As far as I understand the SUBJECT should be set in the DATA section of the SMTP message.
How can I fix this?
If you take a look at this (sorry for a cached link, but Oracle seems to have this link (re)moved or something):
- http://webcache.googleusercontent.com/search?q=cache:CNbquKi8OesJ:forums.sun.com/thread.jspa%3FthreadID%3D5360448+setSubject+commons+email+doesn't+work&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a
it seems that Apache Commons somehow influences Java Mail. The example you provided seems to be a standard Java Mail way of sending mails. So, can you try to either:
- Remove commons-email JAR from your classpath and try the above again,
- Or use other examples e.g. from here: http://www.go4expert.com/forums/showthread.php?t=3649
- Use the (first) example from commons email site: http://commons.apache.org/email/userguide.html
I have the same problem.
I remove library
<artifactId>geronimo-javamail_1.4_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
from my project.
It's work for me!
精彩评论