[英]Java mail : receiving email body as .htm file attachment along with attachment and body
亲爱,
使用Java邮件(Microsoft Exchange服务器)发送电子邮件时,遇到在移动设备中重复的邮件正文问题。 发送电子邮件正文和pdf作为附件,但是当客户在收件箱中接收到邮件时,电子邮件正文内容将被复制(两次),同时发送PDF和一个.htm文件作为附件。 由于.htm文件,电子邮件正文出现了两次。 如何避免邮件中出现此重复正文。 以下是用于发送电子邮件的代码。 此问题在基于浏览器的电子邮件客户端中不会发生,仅在移动设备中会发生。
如下设置电子邮件正文(html内容)
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());
谁能帮助解决这个问题?
输出进入邮件正文:
嗨,这是一个测试。
嗨,这是一个测试
这取决于您发送的格式以及客户端根据该格式显示或构建的内容。
有:
因此,可能的电子邮件可能具有以下来源:
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内容可能会更改为附件(可能是您的问题)。
因此,由于我们,您的问题很难回答:
您必须做进一步的故障排除:
您的多部分内容正确吗? 上网查资料,例如在一些例子在这里 :
包net.codejava.mail;
导入java.io.IOException; 导入java.util.Date; 导入java.util.Properties;
导入javax.mail.Authenticator; 导入javax.mail.Message; 导入javax.mail.MessagingException; 导入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.MimeMessage; 导入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(); } }
}
代替此行: mbp.setContent("Hi, This is a test.", "text/html; charset=utf-8");
这样写: mbp.setContent("Hi, This is a test.", "text/plain; charset=utf-8");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.