Java邮件附件原始内容附加到邮件正文而不是文件

时间:2014-06-11 11:27:59

标签: java javamail sendmail

我发送带有pdf文件作为附件的邮件,收到的邮件是邮件原始内容,即所有

Please find the attached file.

Best Regards ,
Smith
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_73109.1402484524135"

------=_Part_0_73109.1402484524135
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

Dear Sir ,

Please find the attached file.

Best Regards ,
Smith

------=_Part_0_73109.1402484524135
Content-Type: application/pdf; name=QuoteStatment_1402408005919.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=QuoteStatment_1402408005919.pdf

JVBERi0xLjQKJcfl9OXwCjEgMCBvYmoKPDwKL0ZpbHRlciAvRmxhdGVEZWNvZGUKL0xlbmd0aCA2
OTAxCj4+CnN0cmVhbQp42u1d/XPbNrb9uf4rMLPt223GUQkQIADP7MxTZDlR15YcS2nabXfesDYT
q6uvSnQS969ffokARZoCokvqdTZNE8tXwuE5uOARyQuCv5/8fvKV0+Fo+/fm5Yn+6/r9CUZO9Cf+

------=_Part_0_73109.1402484524135--

公开,文件内容附加到邮件正文。

以下是发送邮件前执行操作的高级视图

  1. 通过webservice检索html文件(将采用Base64编码)

  2. 解码为String对象,传递给pd4ml for pdf generation as byte []

  3. 将byte []附件添加到Multipart(MIMEBodyPart)

  4. 使用java邮件1.4.7 API,JDK 1.6。任何帮助都会非常感激。

    以下是邮寄代码段

    public boolean sendEmail(String to_recipients, String cc_recipients, String subject, String bodyHTMLContent, 
            byte[] attachmentContentByteArr, String attachmentFileName) throws QuoteToPDFEmailerException{
        if (logger.isDebugEnabled()) {
            logger.debug("sendEmail(String[], String, String, String, byte[], String) - start"); 
        }
    
        boolean mailSent = Boolean.FALSE;
        boolean debug = Boolean.FALSE;
    
        Session session=null;
    
    
        try {
    
            session = EmailHandler.configureMailerSession();
    
            String debugFlagStr=(resource.getValue("mail.debug"));
    
            String auth=resource.getValue("mail.smtp.auth");
    
            if(EmailHandler.password==null || EmailHandler.password.isEmpty())
            {
                logger.warn("password is empty");
            }
    
            if(debugFlagStr.equalsIgnoreCase(UtilityConstants.STR_TRUE))
            {
                debug = Boolean.TRUE;
            }
    
            session.setDebug(debug);
    
            Message msg = new MimeMessage(session);
    
            InternetAddress addressFrom = new InternetAddress(fromAdd);
            msg.setFrom(addressFrom);
    
            if (to_recipients != null && !to_recipients.isEmpty()) {
    
                String[] recipientArr = to_recipients.split(";");
    
                InternetAddress[] addressTo = new InternetAddress[recipientArr.length];
                for (int i = 0; i < recipientArr.length; i++) {
                    addressTo[i] = new InternetAddress(recipientArr[i]);
                }
    
                msg.setRecipients(Message.RecipientType.TO, addressTo);
    
                if(cc_recipients != null && !cc_recipients.isEmpty()){
                    String[] cc_recipientArr = cc_recipients.split(";");
    
                    InternetAddress[] addressCC = new InternetAddress[cc_recipientArr.length];
                    for (int i = 0; i < cc_recipientArr.length; i++) {
                        addressCC[i] = new InternetAddress(cc_recipientArr[i]);
                    }
    
                    msg.setRecipients(Message.RecipientType.CC, addressCC);
                }
    
                msg.setSubject(subject);
    
                Multipart mp = new MimeMultipart();
                MimeBodyPart htmlPart = new MimeBodyPart();
                htmlPart.setContent(bodyHTMLContent, (resource.getValue("interface.bodyHTMLContent")));
                mp.addBodyPart(htmlPart);
    
                if (attachmentContentByteArr != null && attachmentContentByteArr.length > 0) {
                    logger.info("File Name For The Attachments : "+attachmentFileName);
                    MimeBodyPart attachment = new MimeBodyPart();                   
                    if(!attachmentFileName.endsWith(UtilityConstants.PDF_FILE_DOT_EXTENSION))
                        attachmentFileName = attachmentFileName.concat(UtilityConstants.PDF_FILE_DOT_EXTENSION);
    
                    attachment.setFileName(attachmentFileName);
                    attachment.setContent(attachmentContentByteArr, "application/pdf");
                    mp.addBodyPart(attachment);
                }
    
                msg.setContent(mp);
    
                SMTPTransport smtpTransport = EmailHandler.getConnectedSMTPTransport(auth);
    
                smtpTransport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
    
                mailSent = Boolean.TRUE;
                logger.info("Email sent to recipient successfully");
    
            }
    
    
        }catch (Exception e) {
            logger.error(MessageConfiguration.getMessage(Quote2PDF_EmailAssistConstants.ERROR_MSG_09), e);
            throw new QuoteToPDFEmailerException(MessageConfiguration.getMessage(Quote2PDF_EmailAssistConstants.ERROR_MSG_09), e);
        }
    
        if (logger.isDebugEnabled()) {
            logger.debug("sendEmail(String[], String, String, String, byte[], String) - end"); //$NON-NLS-1$
        }
        return mailSent;
    }
    

0 个答案:

没有答案