尝试使用Java通过Outlook发送时,未明确定义文件格式

时间:2017-02-12 20:44:18

标签: java excel outlook smtp mime

我正在使用此代码通过Outlook使用Java发送excel文件。

excel是通过另一个Java Code生成的。它已经正确生成了。

当我使用此代码时,收件人会收到未定义格式的附件。请帮助

try {
              msg.setFrom();
              java.util.Date date=new java.util.Date();
              fromAddress = new InternetAddress(from);
              msg.setFrom(fromAddress);
              //msg.addRecipient(Message.RecipientType.CC, to);

              msg.setSubject("For Testing Purpose "+date);
              //msg.setSentDate(new Date());
              msg.setText("Attachment");
              /*
              * ************************Test for
              * attachment*************************
              */
             String fileAttachment = "D://testing.xlsx";
              // create the message part
              MimeBodyPart messageBodyPart = new MimeBodyPart();
              MimeBodyPart attachmentBodyPart = new MimeBodyPart();

             String bodytext = "Hi,<br>";
           bodytext = bodytext +"<br>";
              bodytext = bodytext
                      + "Please find Attachnment for last week Reviewr's "+date
                      + "<br>"+"</Font><br><br><br>Thanks<br>Gulshan Raj";                               

             messageBodyPart.setContent(bodytext , "text/html");

              // Part two is attachment
              DataSource source = new FileDataSource(fileAttachment);
             attachmentBodyPart.setDataHandler(new DataHandler(source));
             attachmentBodyPart.setFileName("Reviewers Data");

              // Put parts in message
              Multipart multipart = new MimeMultipart();
              multipart.addBodyPart(messageBodyPart);
              multipart.addBodyPart(attachmentBodyPart);
              msg.setContent(multipart);
              /* ******************************************************************** */
              Transport.send(msg);

              System.out.println("2^^^^^^^fileAttachment "+fileNm);
              System.out.println("2^^^^^^^bodytext  "+bodytext);
        } catch (MessagingException mex) {
              System.out.println("2^^^^^^^^^^^^^^^^^^^send failed, exception: "+ mex);
        }

0 个答案:

没有答案