我正在尝试创建一个发送带有模板化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("info@hexagon.com.ph", "info@dm1nhgc001");//userid and password for "from" email address
}
};
// props.setProperty("mail.user", "m.corbes@hexagon.com.ph");
// 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 :(得分:0)
您可以使用:
new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").getPath());
或
new File(ClassLoader.getSystemClassLoader().getResource("jasperreports/OvertimeIndividual.pdf").toURI());
答案 1 :(得分:0)
你可以这样使用:
new File(AttachMailUtil.class.getResource("/jasperreports/OvertimeIndividual.pdf").getPath());