Java邮件:将电子邮件正文作为.htm文件附件以及附件和正文接收
亲爱的 使用java mail(Microsoft Exchange server)发送电子邮件时,手机中存在邮件正文重复的问题。发送电子邮件正文和pdf作为附件,但当客户收到收件箱中的邮件时,电子邮件正文内容会重复(两次),同时发送pdf和一个.htm文件作为附件。由于.htm文件,电子邮件正文将出现两次。如何避免邮件正文重复。下面是用于发送电子邮件的代码。这个问题不会发生在基于浏览器的电子邮件客户端上,它只发生在移动设备上 设置电子邮件正文(html内容),如下所示Java邮件:将电子邮件正文作为.htm文件附件以及附件和正文接收,java,jakarta-mail,exchange-server,Java,Jakarta Mail,Exchange Server,亲爱的 使用java mail(Microsoft Exchange server)发送电子邮件时,手机中存在邮件正文重复的问题。发送电子邮件正文和pdf作为附件,但当客户收到收件箱中的邮件时,电子邮件正文内容会重复(两次),同时发送pdf和一个.htm文件作为附件。由于.htm文件,电子邮件正文将出现两次。如何避免邮件正文重复。下面是用于发送电子邮件的代码。这个问题不会发生在基于浏览器的电子邮件客户端上,它只发生在移动设备上 设置电子邮件正文(html内容),如下所示 import javax
import javax.mail.Message;
Message msg = new SMTPMessage(session);
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mbp = null;
mbp = new MimeBodyPart();
mbp.setContent("Hi, This is a test.", "text/html; charset=utf-8");
mp.addBodyPart(mbp);
将pdf设置为附件
MimeBodyPart mbp = null;
ByteArrayDataSource xfds3 = null;
mbp = new MimeBodyPart();
byte[] b = //PDF byte array
xfds3 = new ByteArrayDataSource(b, "application/pdf");
mbp.setDataHandler(new DataHandler(xfds3));
String maskName = maskingNo(fileName, prop);
mbp.setFileName(maskName);
mp.addBodyPart(mbp);
msg.setContent(mp);
transport.sendMessage(msg, msg.getAllRecipients());
有人能帮助解决这个问题吗
输出来自邮件正文:
嗨,
这是一个测试
嗨,
这是一个测试这取决于您发送的格式以及客户端显示的内容或基于此构建的内容 有:
- 纯/文本(无花哨的样式)
- 文本/html
- Richtext(由于winmail.dat问题,通常应避免使用)
- 多部分/混合
multipart/mixed
multipart/alternative (holding the two forms of the body part)
text/plain
text/html
text/plain or image/gif (the attachment)
但是,根据邮件客户端的不同,此电子邮件可能只显示纯文本元素,而不显示任何“结构”。如果经常使用上述内容,则会有一些兼容性,因此,如果电子邮件客户端无法处理HTML电子邮件,用户仍然可以读取纯文本。如果客户端无法理解HTML(或者如果多部分部分被破坏),HTML内容可能会被更改为附件(可能是您的问题)
因此,您的问题不容易回答,因为我们:
- 不知道哪个Exchange服务器发送这些电子邮件
- 发送电子邮件的格式是什么
- 如果有什么东西在发送给发件人的过程中更改了格式
- 接收器使用的是哪个客户端
- 当您向内部用户发送电子邮件时是否会发生这种情况
- 当你向Gmail、Outlook.com等发送电子邮件时会发生这种情况吗
- 当您发送没有HTML内容(仅附件)的电子邮件时是否会发生这种情况
- 您的多部件部分正确吗?查看internet上的一些示例,例如:
包net.codejava.mail
导入java.io.IOException;
导入java.util.Date;
导入java.util.Properties
导入javax.mail.Authenticator;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.PasswordAuthentication;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.AddressException;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart
公共类EmailAttachmentSender{
}public static void sendEmailWithAttachments(String host, String port, final String userName, final String password, String toAddress, String subject, String message, String[] attachFiles) throws AddressException, MessagingException { // sets SMTP server properties Properties properties = new Properties(); properties.put("mail.smtp.host", host); properties.put("mail.smtp.port", port); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.user", userName); properties.put("mail.password", password); // creates a new session with an authenticator Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; Session session = Session.getInstance(properties, auth); // creates a new e-mail message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(userName)); InternetAddress[] toAddresses = { new InternetAddress(toAddress) }; msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); msg.setSentDate(new Date()); // creates message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(message, "text/html"); // creates multi-part Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // adds attachments if (attachFiles != null && attachFiles.length > 0) { for (String filePath : attachFiles) { MimeBodyPart attachPart = new MimeBodyPart(); try { attachPart.attachFile(filePath); } catch (IOException ex) { ex.printStackTrace(); } multipart.addBodyPart(attachPart); } } // sets the multi-part as e-mail's content msg.setContent(multipart); // sends the e-mail Transport.send(msg); } /** * Test sending e-mail with attachments */ public static void main(String[] args) { // SMTP info String host = "smtp.gmail.com"; String port = "587"; String mailFrom = "your-email-address"; String password = "your-email-password"; // message info String mailTo = "your-friend-email"; String subject = "New email with attachments"; String message = "I have some attachments for you."; // attachments String[] attachFiles = new String[3]; attachFiles[0] = "e:/Test/Picture.png"; attachFiles[1] = "e:/Test/Music.mp3"; attachFiles[2] = "e:/Test/Video.mp4"; try { sendEmailWithAttachments(host, port, mailFrom, password, mailTo, subject, message, attachFiles); System.out.println("Email sent."); } catch (Exception ex) { System.out.println("Could not send email."); ex.printStackTrace(); } }
setContent(“嗨,这是一个测试。”,“text/html;charset=utf-8”);
写下:
mbp.setContent(“嗨,这是一个测试。”,“text/plain;charset=utf-8”);
所有移动客户端还是只有一个?哪一个?邮件是从Exchange还是其他邮件服务读取的?如果您使用JavaMail阅读消息,它是否具有您期望的结构和内容?