在我的应用程序中,我想将html模板发送给用户电子邮件。当我以编程方式创建html时,Everithing能够正常工作,但是现在我想要做的是从我的应用程序的文件中读取html文本并发送它。我得到一个FileNotFoundException,并且我不知道如何找到.txt文件。看代码:如何在glassfish v3.0中找到.txt文件的路径
public void sendAccountActivationLinkToBuyer(String destinationEmail,
String name) {
// Destination of the email
String to = destinationEmail;
String from = "[email protected]";
try {
Message message = new MimeMessage(mailSession);
// From: is our service
message.setFrom(new InternetAddress(from));
// To: destination given
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Registration succeded");
// Instead of simple text, a .html template should be added here!
message.setText(generateActivationLinkTemplate());
Date timeStamp = new Date();
message.setSentDate(timeStamp);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
private String generateActivationLinkTemplate() {
String htmlText = "";
try {
File f = new File("");
BufferedReader br = new BufferedReader(new InputStreamReader(f.getClass().getResourceAsStream("./web/emailActivationTemplate.txt")));
String content = "";
String line = null;
while ((line = br.readLine()) != null) {
content += line;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return htmlText;
}
第二种方法给我的问题,我无法找到.txt文件。我该怎么办? 我在WebContent文件夹中创建了文件夹Web,Web文件夹现在位于META-INF和WEB-INF旁边(我认为这是适当的地方来放置我的图像,模板,CSS ......)我手动粘贴emailActivationTemplate.txt现在我需要读取它。有任何想法吗?
这是控制台输出:
严重:java.io.FileNotFoundException:\网络\ emailActivationTemplate.txt(系统找不到指定的路径)
到底在哪你放置在文件'emailActivationTemplate.txt'怎么做呢?你可以给我从AppServer根开始的文件的完整路径吗? – adarshr 2011-03-09 13:48:56
目前位于C:\ jee6workspace \ BBS \ WebContent \ web \ emailActivationTemplate.txt 我认为.css文件,图片...应该放在该文件夹中。那是对的吗? – sfrj 2011-03-09 13:53:55
是啊,看到我发布评论后:) – adarshr 2011-03-09 13:54:42