2015-12-15 42 views
0

我試圖創建一個類,發送電子郵件與模板的PDF附加到電子郵件。這是我的代碼。如何檢索項目文件夾內的pdf文件?

import java.io.File; 
import java.util.Properties; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Address; 
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.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

/** 
* 
* @author user 
*/ 
public class AttachMailUtil { 

    public static void sendMail(String to, String from, 
      String subject, String body, boolean bodyIsHTML, File filename) 
      throws MessagingException { 

     // 1 - get a mail session 
     Properties props = new Properties(); 
     // props.put("mail.transport.protocol", "smtp"); 

     props.put("mail.smtp.host", "mail.hexagon.com.ph"); 
     props.put("mail.smtp.port", 587); 
     props.put("mail.smtp.auth", "true"); 

     Authenticator authenticator; 
     authenticator = new Authenticator() { 
      @Override 
      public PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "[email protected]");//userid and password for "from" email address 
      } 
     }; 

     // props.setProperty("mail.user", "[email protected]"); 
     // props.setProperty("mail.password", "password"); 
     // props.put("mail.smtp.host", "localhost"); 
     //props.put("mail.smtp.port", 25);  
     Session session = Session.getDefaultInstance(props, authenticator); 
     session.setDebug(true); 

     // 2 - create a message 
     Message message = new MimeMessage(session); 
     message.setSubject(subject); 
     if (bodyIsHTML) { 
      message.setContent(body, "text/html"); 
     } else { 
      message.setText(body); 
     } 

     // 3 - address the message 
     Address fromAddress = new InternetAddress(from); 
     Address toAddress = new InternetAddress(to); 
     message.setFrom(fromAddress); 
     message.setRecipient(Message.RecipientType.TO, toAddress); 

     //sending email with 
     MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 

     // File savePath = new File(getServletContext().getRealPath("/jasperreports/OvertimeIndividual.pdf")); 
     filename = new File(getServletContext().getRealPath("/jasperreports/WorkOrder.pdf")); 
     // String filename = "SendAttachment.java";//change accordingly 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart2.setDataHandler(new DataHandler(source)); 
     messageBodyPart2.setFileName("OvertimeIndividual.pdf"); 

     //5) create Multipart object and add MimeBodyPart objects to this object  
     Multipart multipart = new MimeMultipart(); 

     multipart.addBodyPart(messageBodyPart2); 

     //6) set the multiplart object to the message object 
     message.setContent(multipart); 

     // 4 - send the message 
     Transport.send(message); 
    } 

} 

,我在這部分

File(getServletContext().getRealPath("/jasperreports/OvertimeIndividual.pdf")); 

我怎樣才能讓我的pdf文件的項目文件夾內的真實路徑有錯誤?任何幫助表示讚賞幫助感謝..

+0

爲什麼不這樣做同樣的方式你爲'WorkOrder.pdf'做了什麼? –

+0

問題是它沒有得到項目內的pdf的路徑先生。即使工作訂單pdf不起作用無法得到真正的路徑:( – slash

+0

你在什麼操作系統? –

回答

0

您可以使用:

new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").getPath()); 

new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").toURI()); 
0

,你可以用這樣的方式:

new File(AttachMailUtil.class.getResource("/jasperreports/OvertimeIndividual.pdf").getPath()); 
相關問題