Sendmail 使用 java 代码,无法添加正文

Sendmail use java code, can't add body text

public void sendMailWithPx() {
    try {
        Process p = Runtime.getRuntime().exec(new String[]{
                getPathSendMail(),
                "-t"
        });
        String base64File = encodeFileToBase64Binary("/Users/jacye/Downloads/test.pdf");//base64 file
        try (OutputStreamWriter osw = new OutputStreamWriter(p.getOutputStream(), "UTF8")) {
            osw.write("Content-Type: application/pdf\n");
            osw.write("From: yourmailtest@testmail.com\n");
            osw.write("To: yourmailtest@testmail.com\n");
            osw.write("Subject: Test send mmail\n");
            osw.write("CC: yourmailtest@testmail.com\n");
            osw.write("BCC: yourmailtest@testmail.com\n");
            osw.write("Content-Disposition: attachment; filename=test.pdf");
            osw.write("\n");
            osw.write("Content-Transfer-Encoding: base64");
            osw.write("\n");
            osw.write(base64File);
            osw.write("this is body");
        }
        p.waitFor();
    } catch (IOException | InterruptedException e) {
        log.error(null, e);
    }
}

private String getPathSendMail() throws IOException {
    Properties prop = new Properties();
    try (InputStream input = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) {
        prop.load(input);
        return prop.getProperty("sendmail.path");
    }
}

我发送带有附件和正文的邮件,但我没有收到正文。

如何发送带有正文和附件的邮件?

如果您需要发送包含正文和附件的邮件,则必须将其作为 MIME Message 发送。 您发送的消息只有一个附件 (PDF)。 我认为您可以在 PDF 中找到“this is body”文本,即使它可能不会被 PDF 查看器呈现。

一个简单的 MIME Message 如下所示

From: John Doe example@example.com
MIME-Version: 1.0
Content-Type:
multipart/mixed; boundary="XXXXboundary text"

This is a multipart message in MIME format.

--XXXXboundary text Content-Type: text/plain

this is the body text

--XXXXboundary text Content-Type: text/plain; Content-Disposition: attachment; filename="test.txt"

this is the attachment text

--XXXXboundary text--