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

Nel*_*elo 36 java jakarta-mail

我正在尝试通过我的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我将此方法称为:

public static void main(String[] args) {
    MailSender mailer=new MailSender();
    mailer.send("friendmail@gmail.com");
}
Run Code Online (Sandbox Code Playgroud)

Rag*_*hav 94

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

new InternetAddress("mail@example.com", "Your Name");
Run Code Online (Sandbox Code Playgroud)


Sar*_*tha 21

您应该使用InternetAddress两个字符串构造函数来传递电子邮件地址和人名.生成的电子邮件将包含一个像Jarrod所示的字符串.

InternetAddress fromAddress=new InternetAddress("my@example.com", "John Doe");
Run Code Online (Sandbox Code Playgroud)


Joh*_*vis 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));

    }
Run Code Online (Sandbox Code Playgroud)