2015-12-15 87 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()); 
相关问题