gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:22:54 28 4
gpt4 key购买 nike

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

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 格式的电子邮件,但是,我无法继续实现该目标,因为我受到了这个阻碍。任何帮助将不胜感激。

编辑

我附加了一个 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 吗?

关于java - 使用 javax mail 发送的电子邮件发送时主题和字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61260320/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com