【问题标题】:Java mail sender's address displayed rather than his name显示 Java 邮件发件人的地址而不是他的姓名
【发布时间】:2012-06-26 16:54:47
【问题描述】:

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

【问题讨论】:

    标签: java jakarta-mail


    【解决方案1】:

    您可以在InternetAddress 中使用

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

    【讨论】:

    • 需要处理java.io.UnsupportedEncodingException
    【解决方案2】:

    您应该使用two string constructor of InternetAddress 来传递电子邮件地址和此人的姓名。生成的电子邮件将包含一个类似于 Jarrod 指示的字符串。

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

    【讨论】:

      【解决方案3】:
          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】:

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

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

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

        【讨论】:

          【解决方案5】:

          上面的答案是正确的,但我发现我需要放置一个 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);
          

          【讨论】:

          • 这是正确的答案,处理异常。
          【解决方案6】:

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

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

          即,

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

          【讨论】:

          • 虽然这在理论上可以回答这个问题,但这并不是一个很好的答案,因为它不教 OP。相反,它给出了一个替代解决方案,没有解释。这通常会导致 OP 不学习,并在出现类似问题时回来提出新问题。您介意添加一些解释吗?
          【解决方案7】:

          您可以通过使用大于和小于符号来强制指定发件人姓名,格式如下:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-03
            相关资源
            最近更新 更多