gpt4 book ai didi

java - 无法在 jsseprovider、gmailsender 中附加文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:49 25 4
gpt4 key购买 nike

我正在尝试这段代码。我可以使用以下代码发送邮件,但是当我插入附件代码时(您可以在//CODE FROM HERE 到//CODE TILL HERE 之间看到),它显示错误

in sending: javax.mail.SendFailedException: No recipient addresses

MimeMessage message = new MimeMessage(session);
try {

DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
//CODE FROM HERE
Multipart emailContent = new MimeMultipart();

//Text body part
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("My multipart text");

//Attachment body part.
MimeBodyPart pdfAttachment = new MimeBodyPart();
clienttargetPdf = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mm.pdf";
pdfAttachment.attachFile(clienttargetPdf);

//Attach body parts
emailContent.addBodyPart(textBodyPart);
emailContent.addBodyPart(pdfAttachment);

//Attach multipart to message
message.setContent(emailContent);

Transport.send(message);
//CODE TILL HERE.

if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {
Log.d("mylog", "Error in sending: " + e.toString());
}

最佳答案

当您已经尝试发送消息时,我想说您的问题就在这里:

    Transport.send(message);
//CODE TILL HERE.

但实际上应该在设置收件人后几行后发送:

//CODE TILL HERE.  

if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message); // <---- here's the second sending

关于java - 无法在 jsseprovider、gmailsender 中附加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55772565/

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