問題描述
JavaMail 無效的 MSGID (JavaMail invalid MSGID)
I am running a JBoss server with underlaying Postfix server. When I am sending emails, JavaMail creates a invalid messageID, e.g. Message-ID: <47112553230.139.4972667128159.JavaMail.undefined>.
I am using this code for sending mails:
Properties props = new Properties();
props.put("mail.smtp.host", "xxx.tld");
props.put("mail.host", "xxx.tld");
InitialContext ictx = new InitialContext(props);
Session sess = (Session) ictx.lookup("java:jboss/mail/Default");
Transport trans = sess.getTransport("smtp");
trans.connect();
MimeMessage msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.addRecipients(RecipientType.TO, InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setText(message);
msg.setHeader("Content-Type", "text/html; charset=\"utf-8\"");
msg.saveChanges();
Transport.send(msg, msg.getAllRecipients());
trans.close();
The main postfix configuration:
myhostname = xxx.tld
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = xxx.tld, Debian-60-squeeze-64-minimal, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = 127.0.0.1
How can I fix this problem?
Best regards, Christian
參考解法
方法 1:
This is how I could fix the problem with the help of @BillShannon:
Properties props = new Properties();
InitialContext ictx = new InitialContext(props);
Session sess = (Session) ictx.lookup("java:jboss/mail/Default");
props = sess.getProperties();
props.put("mail.smtp.host", "xxx.tld");
props.put("mail.host", "xxx.tld");
props.put("mail.from", "yyy@xxx.tld");
sess = Session.getInstance(props);
(by user1565121、user1565121)