gpt4 book ai didi

Javamail 正文与附件一起时不显示

转载 作者:行者123 更新时间:2023-12-01 18:30:44 31 4
gpt4 key购买 nike

所以我遇到了 Javamail 的问题,如果我在邮件中发送附件,正文就会消失。当我不随邮件发送附件时,我只能看到正文。

我的 GMailSender.java:

public GMailSender(String user, String password) {
this.user = user;
this.password = password;

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");

session = Session.getDefaultInstance(props, this);
_multipart = new MimeMultipart();
}


protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}

public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

message.setText(body);
message.setDataHandler(handler);
if(_multipart.getCount() > 0)
message.setContent(_multipart);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);

}

public void addAttachment(String filename) throws Exception
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);

_multipart.addBodyPart(messageBodyPart);
}

我的MainActivity.java

        Button bt_send = findViewById(R.id.Alert);
bt_send.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
BackgroundMail bm = new BackgroundMail(context);
bm.setGmailUserName("***@gmail.com");
bm.setGmailPassword("******");
bm.setMailTo(to);
bm.setFormSubject(value + " DOC/" + mTvResult.getText().toString());
bm.setFormBody("Document Nummer:\n" + mTvResult.getText().toString() + "\n \nDocument Type:\n" + value);
if (images.size() > 0) {
for (Object file : images) {
bm.setAttachment(file.toString());
}
}
bm.setSendingMessage("Loading...");
bm.setSendingMessageSuccess("The mail has been sent successfully.");
bm.send();
}
});

那么如何在添加附件的同时仍然能够看到正文本身?

提前致谢!

最佳答案

您似乎从其他人那里复制了 GMailSender。您应该首先修复所有这些 common mistakes .

您永远不会调用 addAttachment 方法。 (请注意,您可以将该方法替换为 MimeBodyPart.attachFile 方法)。请记住发布您实际使用的代码。

您缺少的关键见解是,对于多部分消息,主体部分必须是多部分中的第一个正文部分。您对 Message.setContent(_multipart); 的调用会覆盖您对 message.setDataHandler(handler); 的调用所设置的消息内容,这也会覆盖由您对 message.setText(body); 的调用。

您需要创建另一个 MimeBodyPart,在该 MimeBodyPart 上设置内容,然后将该 MimeBodyPart 添加为多部分的第一部分,而不是在消息上设置该内容。

关于Javamail 正文与附件一起时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60171582/

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