Java邮件:接收电子邮件正文作为.htm文件附件以及附件和正文

时间:2017-07-08 13:35:00

标签: java javamail exchange-server

逗人,

在使用java邮件(Microsoft Exchange Server)发送电子邮件时,面对在移动设备中复制邮件正文的问题。将电子邮件正文和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());

有人可以帮忙解决这个问题吗?

输出来自邮件正文:

您好, 这是一个考验。

您好, 这是一个测试

2 个答案:

答案 0 :(得分:0)

这取决于您发送的格式以及客户端基于该格式显示或构建的内容。

有:

  • plain / text(no fancy Styling)
  • 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)

enter image description here

但是,根据邮件客户端,此电子邮件可能只显示纯文本元素,而根本不显示“结构”。上面的这些东西如果经常用于具有一些兼容性,那么如果电子邮件客户端无法处理HTML电子邮件,则用户仍然可以阅读纯文本。如果客户端无法理解HTML(或者多部分部分已损坏),则HTML内容可能会更改为附件(可能是您的问题)。

所以你的问题不容易回答我们:

  • 不知道哪个Exchange Server发送了这些电子邮件
  • 使用哪种格式发送电子邮件
  • 如果某些内容正在改变发送方式
  • 的格式
  • 使用接收器的客户端

必须在您身边进行进一步的故障排除:

  • 当您向内部用户发送电子邮件时会发生这种情况
  • 当您向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();
        }
    }
    

    }

答案 1 :(得分: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");