簡體   English   中英

Java郵件:以.htm文件附件以及附件和正文的形式接收電子郵件正文

[英]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());

誰能幫助解決這個問題?

輸出進入郵件正文:

嗨,這是一個測試。

嗨,這是一個測試

這取決於您發送的格式以及客戶端根據該格式顯示或構建的內容。

有:

  • 普通/文本(無花式樣式)
  • text / 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 Server發送那些電子郵件
  • 哪種格式用於發送電子郵件
  • 如果某件東西正在更改發送方的格式
  • 使用接收方的客戶端

您必須做進一步的故障排除:

  • 當您向內部用戶發送電子郵件時會發生這種情況
  • 當您向Gmail,Outlook.com等發送電子郵件時,會發生這種情況嗎?
  • 當您發送不包含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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM