Java邮件发送者的地址显示而非姓名

53

我正在尝试通过我的Java Mail应用程序向我的朋友发送电子邮件。我能够成功地发送邮件,但是收件人列中显示的是完整的电子邮件地址而不是发件人的姓名。我尝试更改各种参数,但仍然无法使邮箱显示发件人的姓名而不是完整的电子邮件地址。

使用以下方法发送消息:

 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");
}
7个回答

118

您可以使用 InternetAddress 来设置名称。

new InternetAddress("mail@example.com", "Your Name");

需要处理 java.io.UnsupportedEncodingException - Brethlosze

24
你应该使用InternetAddress的双字符串构造函数,以便同时传递电子邮件地址和人名。生成的电子邮件将包含像Jarrod所指示的字符串。
InternetAddress fromAddress=new InternetAddress("my@example.com", "John Doe");

5
    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));

    }

4

发件人字段的显示方式是客户端特定的实现细节。

通常,如果发送者的格式为"发送者姓名" <sender@domain.com>,则客户端将根据配置执行正确的操作。

如果缺少名称信息,则某些客户端将从其通讯录信息中推断名称信息。


4

以上的回答是正确的,但我发现我需要加上 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);

这是正确的答案,处理异常。 - Brethlosze

3
您可以通过使用大于号和小于号符号 < > 来强制指定发件人的名称,格式如下:
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等...)

2

在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();  

 }

虽然这个回答在理论上可能解决了问题,但它并不是一个好的回答,因为它没有教会提问者。相反,它给出了一种没有解释的替代方案。这通常会导致提问者没有学到东西,并在类似的问题再次出现时返回提问新问题。您介意添加一些解释吗? - Vogel612

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接