2017-07-08 11 views
0

ディアーズ、Javaのメール(Microsoft Exchangeのサーバー)を使用して電子メールを送信する際に、モバイルで複製問題のメール本文に直面のJavaメール:添付ファイルと身体

とともに.htmのファイルの添付ファイルとして電子メールの本文を受けます。電子メール本文とpdfを添付ファイルとして送信しますが、顧客が受信ボックスでメールを受信すると、電子メール本文の内容が複製され(2回)、送信PDFと添付ファイルとして1つの.htmファイルが複製されます。 .htmファイルのため、メールの本文は2回になります。メールでこの重複本文を避ける方法。以下は電子メールの送信に使用されるコードです。この問題は、ブラウザベースの電子メールクライアントでは起こりません。その問題はモバイルでしか起こりません。

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); 

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()); 

は、誰もがこの問題を解決する方法を助けることができる添付ファイルとしてPDFを設定する以下のように電子メールの本文(HTMLコンテンツ)を設定しますか?

出力は、メール本文に来ている:

こんにちは、 これはテストです。

こんにちは、 これはこれは、それに基づいて、あなたが送信され、クライアントが表示またはビルドされたどのような形式に依存してテスト

+0

すべてのモバイルクライアントまたは一つだけ?どれ?メッセージがExchangeまたは他のメールサービスから読み取られていますか? JavaMailを使用してメッセージを読んだら、期待どおりの構造と内容を持っていますか? –

答えて

0

です。

あります

  • プレーン/テキスト(なしファンシースタイリング)
  • text/htmlの
  • リッチテキスト(通常は原因の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) 

enter image description here

ただし、メールクライアントに応じて、このメールは、すべてのプレーンテキスト要素なし「構造」を表示することがあります。そのようなものは、しばしばその場で互換性を持つために使用される場合、電子メールクライアントがHTML電子メールを処理できない場合、ユーザーはプレーンテキストを読み取ることができます。クライアントがHTMLを理解できない場合(またはマルチパートセクションが壊れている場合)、HTMLコンテンツが添付ファイルに変更される可能性があります(問題の可能性があります)。

だから簡単isn'tあなたの質問は、私たちのように答えるために:

  • Exchange Serverが何かした場合の電子メール
  • を送信するために使用されているフォーマットこれらのメール
  • を送信しているか分からないのですか送信者、受信機が使用されているクライアント

さらにtroubleshooへの道上の形式を変更していますティンはあなたの側で行う必要があります:

  • ですあなたは
  • は、内部ユーザーに電子メールを送信する際に起こっていることが起こって、あなたがGmailに電子メールを送信する場合、Outlook.com、...
  • HTMLコンテンツ(添付ファイルのみ)なしでメールを送信すると、それが起こっていますか?
  • マルチパートセクションは正しいですか?インターネット上のいくつかの例を確認してください。例:here

    パッケージnet.codejava.mail;

    import java.io.IOException; import java.util.Date; import java.util.Properties;

    import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import 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(); 
        } 
    } 
    

    }

0

mbp.setContent("Hi, This is a test.", "text/html; charset=utf-8"); この書き込み: mbp.setContent("Hi, This is a test.", "text/plain; charset=utf-8");

関連する問題