显示Java邮件发件人的地址而不是他的名字

时间:2012-06-26 16:54:48

标签: java javamail

我正在尝试通过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 个答案:

答案 0 :(得分:87)

您可以使用

InternetAddress中设置名称
new InternetAddress("mail@example.com", "Your Name");

答案 1 :(得分:21)

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

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

答案 2 :(得分:4)

如何显示from字段是客户特定的实现细节。

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

如果缺少地址簿信息,某些客户会从其地址簿信息中推断出名称信息。

答案 3 :(得分:3)

上面的答案是正确的,但我发现我需要尝试抓住它才能工作,这是我在sendemailwebapp演示应用程序中找到的工作。

消息msg =新的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);

答案 4 :(得分: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));

    }

答案 5 :(得分:0)

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

 }

答案 6 :(得分:0)

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

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