JAVA邮件API : Unable to attach xls file to Mail

标签 java jakarta-mail attachment

我的下面的 Java 类必须使用 java 邮件将文本文件内容和邮件以及附加的 xls 文件复制到收件人。

现在我可以读取并邮寄文本文件内容,但无法附加 xls 文件。

以下是我的片段:

static void sendmail() throws IOException,     
MessagingException,AddressException,FileNotFoundException
   {

          DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy 
HH:mm:ss");
          Calendar cal = Calendar.getInstance();
            String to1=getOrderinfo.to1;
            String to2 = getOrderinfo.to2;
            String to3 = getOrderinfo.to3; 
            String to4 = getOrderinfo.to4;
            String from =getOrderinfo.from;
            String host = getOrderinfo.host;
            Properties properties = System.getProperties();
            properties.setProperty("mail.smtp.host", host);
            Session session = Session.getDefaultInstance(properties);
            MimeMessage message = new MimeMessage(session); 
             Multipart multipart = new MimeMultipart();



            String pathLogFile = "E:/car_failed_report/log.txt";

            try {

                    message.setFrom(new InternetAddress(from));   
                    message.addRecipient(Message.RecipientType.TO, new 
InternetAddress(to1));
                   message.setSubject(" CAR NOT YET INTEGRATED REPORT at 
: "+dateFormat.format(cal.getTime()));
                    StringBuffer sb = new StringBuffer();
                    FileInputStream fstream = new 
FileInputStream(pathLogFile);
                    BufferedReader br = new BufferedReader(new 
InputStreamReader(fstream));

                    String singleLine;
                    while ((singleLine = br.readLine()) != null) 
                    {

                        sb.append(singleLine + "<br>");
                       // CarParser1.sb1.append(singleLine +"<br>");
                    }
                    br.close();
                    String allLines;

                   allLines = sb.toString();


                    String allLines_html=" <html><head><title></title> 
</head>"
                            + "<body >"+allLines+"</body ></html>";
                  message.setContent(allLines_html, "text/html; 
charset=ISO-8859-1");

                 MimeBodyPart attachPart = new MimeBodyPart();
                File attachement = new File("E:\\car_failed_report
\\failed.xls");
                if (attachement.exists()) {
                    attachPart.attachFile(attachement);
                } else {
                    System.out.println("ERROR READING THE FILE");
                    throw new FileNotFoundException();
                }

                 Transport.send(message);



                System.out.println("Email Sent successfully....");


                System.out.println();

            } 
            catch(FileNotFoundException f1)
            {
                System.out.println("File not yet created..this is from 
mailer class");

                return;
            }
            catch (MessagingException mex) 
            {
                System.out.println("Invalid Email Address.please provide 
a valid email id to send with");
                mex.printStackTrace();


            }

谁能帮我附上 xls 文件吗?

提前致谢。

最佳答案

您创建了带有附件的正文部分,但未将其添加到邮件中。替换以 message.setContent 开头的代码:

            MimeMultipart mp = new MimeMultipart();
            MimeBodyPart body = new MimeBodyPart();
            body.setText(allLines_html, "iso-8859-1", "html");
            mp.addBodyPart(body);

            MimeBodyPart attachPart = new MimeBodyPart();
            File attachement = new File("E:\\car_failed_report\\failed.xls");
            if (attachement.exists()) {
                attachPart.attachFile(attachement);
                mp.addBodyPart(attachPart);
            } else {
                System.out.println("ERROR READING THE FILE");
                throw new FileNotFoundException();
            }
            message.setContent(mp);

关于JAVA邮件API : Unable to attach xls file to Mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006763/

相关文章:

java - 类如何从其包含复合类的父类(扩展 Activity)引用变量?

java - 为什么转换会在本地工作而不是在服务器上工作?

java - Java Mail 标准属性是否有常量? ide/编译器还有其他方法可以检查他们的拼写吗?

java - 在静态方法中模拟 UrlEncoder

在 Tomcat 8 上运行时迭代文件时出现 java.lang.NullPointerException

java/processing 返回 ArrayList 抛出 : This method must return a result of Type ArrayList

java - Java 有 uudecoding 的标准机制吗?

php - 为未注册用户下载 vbulletin 中的附件文件

c# - 如何通过电子邮件发送 Excel 文件?

ios - 如何在 quickblox iOS 中将视频文件作为附件发送?