使用 javax 邮件发送的电子邮件发送时主题和字段为空

Email sent using javax mail delivers with empty subject and to fields

我正在使用以下代码从我的应用程序发送电子邮件:

MimeMessage message = new MimeMessage(session);
String encodingOptions = "text/html; charset=UTF-8";
message.setHeader("Content-Type", encodingOptions);
message.setFrom(new InternetAddress(fromEmailAddress));
String[] toAddressArr = emailMsg.getTo().split(CommonConstants.SEMI_COLON);

for (String srtAddress : toAddressArr) {
    InternetAddress ia = new InternetAddress(srtAddress);
    addressToList.add(ia);
}

InternetAddress[] addressTo = new InternetAddress[addressToList.size()];
message.setRecipients(MimeMessage.RecipientType.TO, addressToList.toArray(addressTo));

if (!Util.isNullOrBlank(emailMsg.getCc())) {
    String[] copyAddressArr = emailMsg.getCc().split(CommonConstants.SEMI_COLON);
    ArrayList<InternetAddress> addressCopyList = new ArrayList<>();
    for (String srtAddress : copyAddressArr) {
    InternetAddress ia = new InternetAddress(srtAddress);
    addressCopyList.add(ia);
}

InternetAddress[] addressCopy = new InternetAddress[addressCopyList.size()];
message.setRecipients(MimeMessage.RecipientType.CC, addressCopyList.toArray(addressCopy));
}

if (!Util.isNullOrBlank(emailMsg.getBcc())) {
    String[] bccAddressArr = emailMsg.getBcc().split(CommonConstants.SEMI_COLON);
    ArrayList<InternetAddress> addressBccList = new ArrayList<>();
    for (String srtAddress : bccAddressArr) {
        InternetAddress ia = new InternetAddress(srtAddress);
        addressBccList.add(ia);
    }

    InternetAddress[] addressBcc = new InternetAddress[addressBccList.size()];
    message.setRecipients(MimeMessage.RecipientType.BCC, addressBccList.toArray(addressBcc));
}

    message.setSubject(emailMsg.getSubject(), "UTF-8");
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(emailMsg.getMessage());
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    }
}

使用上面的代码,我将电子邮件发送到收件人的电子邮件地址,但是,主题字段为空,收件人字段也为空,而所有收件人都显示在电子邮件的密件抄送字段中。此外,附件以字节字符串形式出现,而不是可下载的 pdf 文件。 以下是其中的示例:

user@gmail.com to bcc: user@outlook.com, bcc: user@domain.com

------=_Part_0_455461077.1587067667930 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit

Dear Sir,

STATUS REPORT




For further inquiries, kindly contact our customer services Unit on any of the following numbers: 01-255xxxx and 01-270xxxx.

Kind Regards. For XYZ Corp. ------=_Part_0_455461077.1587067667930 Content-Type: text/plain; name=37855.pdf Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=37855.pdf

JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G bGF0ZURlY29kZSA+PgpzdHJlYW0KeAHNXW1v5EZy/s5fQY9mdmbXEocvwzet5PPKu7eO42ziWInv EOWTkcshsANc/P+BPNX1VL+QlMTROkBgYKludldX13tXF8d/y3/I/5b3dT7mp27M6z7/n//If8r/ Oz9+81uV//xbXrr/fvsZo8qiPmlb/qiGvB2aYqzzn3/N7+6zq7IoS/Tf/wxIbhge1Vi1RdlV+VXd 1F1+/2t+vL+v8yq//0v+b/nhi83r/KrKDxf62Opj9zr79/z+u/zDvUPuyWWjteqyORVD06drZW6t ...

我的意图是发送具有 html 格式的电子邮件,但是,我无法继续 objective,因为我被这个节目阻止者阻止了。 任何帮助将不胜感激。

编辑

我附上了一个 pdf 文件,将作为电子邮件附件发送,代码如下:

if (attachments != null) {  
        for (EmailAttachmentData attachment : attachments) {
            MimeBodyPart attachPart = new MimeBodyPart();
            DataSource source = new ByteArrayDataSource(attachment.getData(),      
            attachment.getFileType());
            attachPart.setDataHandler(new DataHandler(source));
            attachPart.setFileName(attachment.getFileName());
            multipart.addBodyPart(attachPart);
        }
    }

    message.setContent(multipart);
    message.setSentDate(new Date());

    try {
        Transport transport = session.getTransport(mailProtocol);
        transport.connect(mailHost, Integer.parseInt(mailPort), userName, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

这是我对 javax mail 的 maven 依赖定义

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
        <scope>provided</scope>
    </dependency>

您使用的是哪个版本的 JavaMail?

您的收件人代码看起来不错,尽管比必要的要冗长得多。 但是你的内容代码全错了。

带有附件的邮件是包含多部分内容的邮件,其中一个正文部分包含主要邮件内容,另一个正文部分包含附件。

您的代码并未显示您实际发送消息的方式。 你刚刚打电话给 Transport.send 吗?

我终于通过从我的一些 Maven 依赖项中排除 geronimo-javamail_1.4_spec 和 geronimo-activation_1.1_spec 解决了我自己的问题。我也有多个 javax.mail-api 和 javax.mail 依赖项,因此删除重复项挽救了这一天。现在一切正常。主题和收件人字段填充良好,附件也已正确发送。为了清楚起见,我 post 我在下面做出的排除:

    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-impl</artifactId>
        <version>1.2.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-activation_1.1_spec</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-javamail_1.4_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-api</artifactId>
        <version>1.2.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-activation_1.1_spec</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-javamail_1.4_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-dom</artifactId>
        <version>1.2.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-activation_1.1_spec</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-javamail_1.4_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>