Java邮件发送者';显示的是他的地址而不是他的名字

Java邮件发送者';显示的是他的地址而不是他的名字,java,jakarta-mail,Java,Jakarta Mail,我正试图通过Java邮件应用程序向我的朋友发送邮件。我能够成功地做到这一点,但是邮箱中的收件人栏显示的是完整的电子邮件地址,而不是发件人的姓名。我尝试更改各种参数,但邮箱仍然会显示完整的电子邮件地址,而不是发件人的姓名 使用此方法发送消息: public void send(String key){ String to=key; String from="mygmailid"; String subject="wassp"; String text="Hello

我正试图通过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");
}

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

通常,如果发送方的格式为
“发送方名称”
,则客户端将根据配置执行正确的操作

如果地址簿信息缺失,一些客户端会从中推断出姓名信息。

您应该使用传递电子邮件地址和姓名。生成的电子邮件将包含一个类似Jarrod的字符串

InternetAddress fromAddress=new InternetAddress("my@example.com", "John Doe");

您可以使用在
InternetAddress
中设置名称

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

在Try块中尝试此代码。可以在mimessage的setFrom()方法中初始化您的名称

simpleMessage.setFrom(new InternetAddress("Your mail id", "Your name"));


上面的答案是正确的,但我发现我需要加入一个try-catch才能让它工作,下面是我在sendemailwebapp演示应用程序中发现的工作

Message msg=新的mimessage(会话)


您可以按照以下格式使用大于和小于符号<>强制指定发件人的名称:

String from="John Smith<friendmail@gmail.com>";
.
.
.
fromAddress=new InternetAddress(from);
String from=“John Smith”;
.
.
.
fromAddress=新的Internet地址(from);

publicstaticvoidmain(字符串[]args){
MailSender mailer=新的MailSender();
邮寄(“约翰·史密斯”);
}

收到电子邮件时,电子邮件收件人将在其收件箱中看到“John Smith”的名字。(如果指定,大多数电子邮件程序都会显示名称。例如Outlook、gmail、hotmail等。)

虽然这在理论上可以回答这个问题,但实际上并不是一个好的答案,因为它不会教授OP。相反,它给出了一个没有解释的替代解决方案。这通常会导致OP无法学习,当出现类似问题时,会回来问新问题。您介意添加一些解释吗?这是处理异常的正确答案。它需要处理
java.io.UnsupportedEncodingException
    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 {

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

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