简体   繁体   中英

Sender is not set in Email sent using JavaMail api

I have following code. But in the email, I get the sender as " unknown sender ". When the receiver is gmail and yahoo, the email is not received at all. I managed to receive the email from own domain mail box (set up in outlook) and also another company email address (different domain). In this two, sender email is shown but still in outlook when the email is received it says as "unknown sender" and the name is not shown.

在此处输入图片说明

Properties props = new Properties();
        props.put("mail.smtp.host", "mail.domain.com"));

    props.put("mail.smtp.auth","false"));
    props.put("mail.smtp.starttls.enable","false"));
    props.put("mail.smtp.port","25"));

    Session session = Session.getDefaultInstance(props);


    try {
        String subject = "Email Subject";
        MimeMessage message = new MimeMessage(session);
        message.setSender(new InternetAddress("no-reply@domain.com", "Sender Name"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(emailTo));
        message.setSubject(subject);
        String content = writer.toString();
        message.setContent(content, "text/html; charset=UTF-8");

        Transport.send(message);
        logger.debug ("Email sent");
        return true;
    }

There are no exceptions and and I get the log "Email sent"

I don't have a mail box. I want to send the email without sender, still in the email it should be displayed a sender name, as well as sender email address as "no-reply@domain.com"

Following is how it is displayed. I have white marked company sensitive data. domain.com is also mock domain. In the real application I use real domain but the result is same.

在此处输入图片说明

Why gmail and yahoo blocking my mail?

尝试使用setFrom代替setSender ,即:

message.setFrom(new InternetAddress("Sender Name" + "<" + "no-reply@domain.com" + ">"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM