50

我正在尝试通过我的 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");
}
4

7 回答 7

112

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

new InternetAddress("mail@example.com", "Your Name");
于 2012-06-26T17:19:24.027 回答
23

您应该使用InternetAddress 的两个字符串构造函数来传入电子邮件地址和人名。生成的电子邮件将包含一个类似于 Jarrod 指示的字符串。

InternetAddress fromAddress=new InternetAddress("my@example.com", "John Doe");
于 2012-06-26T17:19:16.870 回答
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));

    }
于 2016-09-19T06:51:52.453 回答
4

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

通常,如果发件人是"Sender Name" <sender@domain.com>客户端的形式,将根据配置做正确的事情。

如果丢失,一些客户会从他们的地址簿信息中推断出姓名信息。

于 2012-06-26T17:06:20.243 回答
4

上面的答案是正确的,但我发现我需要放置一个 try catch 才能让它工作,这是我从 sendemailwebapp 演示应用程序中发现的。

消息消息 = 新的 MimeMessage(会话);

    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);
于 2016-09-08T16:50:57.427 回答
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();  

 }
于 2015-10-16T05:31:58.983 回答
1

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

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 等...)

于 2020-03-10T18:00:55.277 回答