java - Javamail 正文与附件一起时不出现
问题描述
所以我有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。您应该从修复所有这些常见错误开始。
您永远不会调用 addAttachment 方法。(请注意,您可以将该方法替换为MimeBodyPart.attachFile方法)。请记住发布您实际使用的代码。
您缺少的关键见解是,对于多部分消息,主体部分必须是多部分中的第一个正文部分。您的呼叫Message.setContent(_multipart);
覆盖了您呼叫设置的消息内容message.setDataHandler(handler);
,这也覆盖了您呼叫设置的内容message.setText(body);
。
您需要创建另一个 MimeBodyPart,在该 MimeBodyPart 上设置内容,然后将该 MimeBodyPart 添加为多部分的第一部分,而不是在消息上设置该内容。
推荐阅读
- cytoscape - Cytoscape 加载麋鹿图表的行为不一致,很多时候我的节点不可见
- javascript - Django 加载静态文件的问题
- python - 从 wordcloud 导出到 Python 列表
- python-3.x - 如何在 VSCODE 和 pylam 中全局禁用 E501
- javascript - 如何检测 mjpeg 流何时停止使用 javascript
- java - 如何重载使用 @RequestBody 的 Spring Boot RestController 方法?
- ruby-on-rails - Rails 模式转储显示 NameError: undefined method `attributes' for class `Nokogiri::XML::AttributeDecl'
- spring-boot - 如何使用 Spring Cloud Sleuth 更改 REST Endpoint 的 Span 名称
- python - FLASK 应用程序:通过另一个脚本从表单发送数据,而一个“输入”是动态段落
- linux - /linux/usr/include/asm-generic/socket.h:123:5:警告:“__BITS_PER_LONG”未定义,计算结果为 0 [-Wundef]