Вопрос

No matter what I try, I can't get the Sender Name to show up in the outgoing mails sent through the javax.mail Session class.

Please note that I'm using Rhino so there's a mixture of javascript and java here, but the code problem exists in pure java as well.

var _java = JavaImporter(javax.mail, javax.mail.internet, java.util);

        var props = new _java.Properties();
        props.put("mail.smtp.host", myHost);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.user", myUserName);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", 465);
        props.put("mail.smtp.socketFactory.class",
                  "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl", "true");

        var session = _java.Session.getInstance(props);

        var message = _java.MimeMessage(session);
        message.setSender(
            new _java.InternetAddress("myEmailAddress", "My Full Name")
        );
        message.setRecipients(
            _java.Message.RecipientType.TO,
            [new _java.InternetAddress("anyAddress@anydomain.com", true)]
        );
        message.setText("Testing");
        message.setSubject("Test");
        message.setSentDate(new Date());
        var t = session.getTransport("smtp");
        t.connect(myUserName, myPassword);
        t.sendMessage(message, message.getAllRecipients());

The email that comes through has:

Sender: Personal Name <email@domain.com>

But the from is always:

From: email@domain.com

Therefore, mail clients like Outlook and Gmail aren't plucking out the name.

I've tried adding

props.put("mail.smtp.from", "Full Name <email@domain.com>")

but this doesn't make any difference.

Anybody have any experience with this bug?

Thanks.

Это было полезно?

Решение

What a pain.

The bug was that I was using message.setSender instead of message.setFrom.

Changing that solved it.

Другие советы

I would like to clear it as I have the same issue

// sender the email string example test@test.com

message.setFrom(new InternetAddress(sender));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top