![](/img/trans.png)
[英]sender's address displayed rather than his name while sending with SimpleMailMessage in java
[英]Java mail sender's address displayed rather than his name
我正在尝试通过我的 Java 邮件应用程序向我的朋友发送邮件。 我能够成功地做到这一点,但是邮箱中的收件人栏显示了完整的电子邮件地址,而不是发件人的姓名。 我尝试更改各种参数,但邮箱仍然会显示完整的电子邮件地址而不是发件人的姓名。
使用此方法发送消息:
public void send(String key){
String to=key;
String from="mygmailid";
String subject="wassp";
String text="Hello";
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "myname");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession=Session.getDefaultInstance(props);
Message simpleMessage=new MimeMessage(mailSession);
InternetAddress fromAddress=null;
InternetAddress toAddress=null;
try{
fromAddress=new InternetAddress(from);
toAddress=new InternetAddress(to);
}
catch(AddressException e){
e.printStackTrace();
}
try{
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO,toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
transport.connect("smtp.gmail.com",465, "myid@gmail.com", "mygmailpassword");
transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
transport.close();
}
catch(MessagingException e){
e.printStackTrace();
}
}
我将此方法称为:
public static void main(String[] args) {
MailSender mailer=new MailSender();
mailer.send("friendmail@gmail.com");
}
您可以使用在InternetAddress
设置名称
new InternetAddress("mail@example.com", "Your Name");
您应该使用InternetAddress的两个字符串构造函数来传入电子邮件地址和人名。 生成的电子邮件将包含一个类似于 Jarrod 指示的字符串。
InternetAddress fromAddress=new InternetAddress("my@example.com", "John Doe");
try {
String from = " EMAIL ID";
String SMTP_AUTH_PWD = " PASSWORD ";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
String SMTP_HOST_NAME = "smtp.gmail.com";
int SMTP_HOST_PORT = 465;
javax.mail.Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = ((javax.mail.Session) mailSession)
.getTransport();
javax.mail.Message message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("", "text/plain");
message.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(receiver));
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, from,
SMTP_AUTH_PWD);
message.setFrom(new InternetAddress(from," YOUR PREFERED NAME "));
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
message.setContent(multipart);
transport.sendMessage(message,
message.getRecipients(javax.mail.Message.RecipientType.TO));
}
from 字段的显示方式是客户端特定的实现细节。
通常,如果发件人采用"Sender Name" <sender@domain.com>
的形式,客户端将根据配置执行正确的操作。
如果缺少姓名信息,一些客户端会从他们的地址簿信息中推断出姓名信息。
上面的答案是正确的,但我发现我需要放置一个 try catch 才能使其工作,这是我从 sendemailwebapp 演示应用程序中发现的工作。
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(userName, "YourName"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(message);
在 try 块中试试这个代码。你可以在 MimeMessage 的 setFrom() 方法中初始化你的名字。
simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name"));
IE,
try{
simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name"));
simpleMessage.setRecipient(RecipientType.TO,toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
transport.connect("smtp.gmail.com",465, "myid@gmail.com", "mygmailpassword");
transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
transport.close();
}
您可以按照以下格式使用大于和小于符号 < > 强制指定发件人姓名:
String from="John Smith<friendmail@gmail.com>";
.
.
.
fromAddress=new InternetAddress(from);
或者
public static void main(String[] args) {
MailSender mailer=new MailSender();
mailer.send("John Smith<friendmail@gmail.com>");
}
收到电子邮件时,电子邮件收件人将在其收件箱中看到姓名“John Smith”。 (如果指定,大多数电子邮件程序会显示名称。例如 Outlook、gmail、hotmail 等...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.