Java邮件:接收电子邮件正文与附件和车身

问题描述:

逗人,Java邮件:接收电子邮件正文与附件和车身

面临的一个问题,邮件正文使用Java邮件(微软Exchange服务器)发送电子邮件时重复进行移动沿着.htm文件附件。发送邮件正文和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()); 

谁能帮助如何解决这个问题呢?

输出在邮件正文未来:

嗨, 这是一个测试。

嗨, 这是一个测试

+0

所有移动客户端或只有一个?哪一个?是从Exchange还是从其他邮件服务中读取邮件?如果您使用JavaMail阅读邮件,它是否具有您期望的结构和内容? –

这取决于您要发送的形式以及客户端显示或建立基于这一点。

有:

  • 纯/文(没有花哨的造型)
  • 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内容可能会变成附件(可能是您的问题)。

所以,你这难道不是容易回答的问题,因为我们:

  • 不知道哪个Exchange服务器发送这些电子邮件
  • 这是用来格式发送出去的电子邮件
  • 如果某样东西正在改变的方式的格式向发送者
  • 使用哪个客户端的接收器

此外troubleshoo婷必须在你的身边来完成:

  • 是,当你发送电子邮件到内部用户
  • 正在发生的事情是发生在您发送电子邮件到Gmail,Outlook.com,...
  • 发送没有HTML内容的电子邮件时发生这种情况(仅附件)?
  • 您的多部分部分是否正确?查看互联网上的一些示例,例如here

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

    }

而是这行: mbp.setContent("Hi, This is a test.", "text/html; charset=utf-8"); 这样写: mbp.setContent("Hi, This is a test.", "text/plain; charset=utf-8");