在 javax.mail.MimeMessage 中设置发件人名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1567467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:10:24  来源:igfitidea点击:

Setting the from name in a javax.mail.MimeMessage?

javaemailjavax.mailmime-message

提问by abeger

Currently, our application uses a javax.mail to send email, using javax.mail.MailMessage. We set the From headers of the email this way:

目前,我们的应用程序使用 javax.mail 发送电子邮件,使用 javax.mail.MailMessage。我们以这种方式设置电子邮件的发件人标题:

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("[email protected]"));

This works just fine, but we'd like to make the "From" section a little more user-friendly. Currently, someone receiving an email will see "[email protected]" in the "From" section of their inbox. Instead, we'd like them to see "Company XYZ" there. I figure this is probably done with the addHeader() method, but I'm not sure what the header name would be.

这工作得很好,但我们想让“发件人”部分更加用户友好。目前,收到电子邮件的人会在收件箱的“发件人”部分看到“[email protected]”。相反,我们希望他们在那里看到“公司 XYZ”。我认为这可能是通过 addHeader() 方法完成的,但我不确定标头名称是什么。

采纳答案by abeger

OK, reading documentation about ALL the classes involved would have been helpful. The correct syntax should be

好的,阅读有关所有相关类的文档会有所帮助。正确的语法应该是

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("[email protected]", "Company XYZ"));

Source: https://javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html

来源:https: //javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html

回答by checklist

If you want to store the email + the name in one string (easier than keeping two string):

如果要将电子邮件 + 姓名存储在一个字符串中(比保留两个字符串更容易):

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress("Company XYZ <[email protected]>"));

回答by Adrian Adzik

In case when I used localized text with special characters like \u00FA I had problems with encoding email address alias for some pop3 clients if I'd used just

如果我使用带有特殊字符(如 \u00FA)的本地化文本,我在为某些 pop3 客户端编码电子邮件地址别名时遇到问题,如果我只使用过

MimeMessage m = new MimeMessage(session);
m.setFrom();

It can be resolved by separate email address and alias by invoke:

可以通过调用单独的电子邮件地址和别名来解析:

MimeMessage m = new MimeMessage(session);
            m.setFrom(new InternetAddress(session.getProperty("mail.from"), session.getProperty("mail.from.alias"),"UTF8"));

ref: https://javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html#InternetAddress(java.lang.String,%20java.lang.String,%20java.lang.String)

参考:https: //javamail.java.net/nonav/docs/api/javax/mail/internet/InternetAddress.html#InternetAddress(java.lang.String,%20java.lang.String,% 20java.lang.String)

回答by Rodrigo Turassa

ic = new InitialContext();

final Session session = (Session) ic.lookupLink(snName);
final Properties props = session.getProperties();

props.put("mail.from", mailFrom); //[email protected]
props.put("mail.from.alias", mailName);//"joao Ninguem"

// Create a message with the specified information.
final MimeMessage msg = new MimeMessage(session);
msg.setSubject(subject);
msg.setSentDate(new Date());

msg.setFrom(new InternetAddress(session.getProperty("mail.from"), session.getProperty("mail.from.alias"), "UTF8"));


msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo, false));
msg.setContent(body, "text/html");

// Create a transport.
Transport.send(msg);