我正在使用JavaMail编写一个简单的应用程序,当在一个目录中找到一些文件时,它可以发送电子邮件。我已经成功地从Eclipse中实现了这个功能。我运行了应用程序,并且没有出现任何错误就成功地发送了电子邮件。
但是,当我创建jar文件并执行它时,在发送电子邮件部分失败了。它会抛出以下异常:
我正在从属性文件中加载这些值。
但是,当我创建jar文件并执行它时,在发送电子邮件部分失败了。它会抛出以下异常:
javax.mail.NoSuchProviderException: No provider for smtp
at javax.mail.Session.getProvider(Session.java:460)
at javax.mail.Session.getTransport(Session.java:655)
at javax.mail.Session.getTransport(Session.java:636)
at main.java.util.MailManager.sendMail(MailManager.java:69)
at main.java.DownloadsMail.composeAndSendMail(DownloadsMail.java:16)
at main.java.DownloadsController.checkDownloads(DownloadsController.java:51)
at main.java.MainDownloadsController.run(MainDownloadsController.java:26)
at java.lang.Thread.run(Unknown Source)
我正在使用以下方法中的库:
public static boolean sendMail(String subject, String text) {
noExceptionsThrown = true;
try {
loadProperties();
} catch (IOException e1) {
System.out.println("Problem encountered while loading properties");
e1.printStackTrace();
noExceptionsThrown = false;
}
Properties mailProps = new Properties();
String host = "mail.smtp.host";
mailProps.setProperty(host, connectionProps.getProperty(host));
String tls = "mail.smtp.starttls.enable";
mailProps.setProperty(tls, connectionProps.getProperty(tls));
String port = "mail.smtp.port";
mailProps.setProperty(port, connectionProps.getProperty(port));
String user = "mail.smtp.user";
mailProps.setProperty(user, connectionProps.getProperty(user));
String auth = "mail.smtp.auth";
mailProps.setProperty(auth, connectionProps.getProperty(auth));
Session session = Session.getDefaultInstance(mailProps);
//session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(messageProps.getProperty("from")));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
messageProps.getProperty("to")));
message.setSubject(subject);
message.setText(text);
Transport t = session.getTransport("smtp");
try {
t.connect(connectionProps.getProperty("user"), passwordProps
.getProperty("password"));
t.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
System.out.println("Error encountered while sending the email");
e.printStackTrace();
noExceptionsThrown = false;
} finally {
t.close();
}
} catch (Exception e) {
System.out.println("Error encountered while creating the message");
e.printStackTrace();
noExceptionsThrown = false;
}
return noExceptionsThrown;
}
我正在从属性文件中加载这些值。
mail.smtp.host=smtp.gmail.com
mail.smtp.starttls.enable=true
mail.smtp.port=587
mail.smtp.auth=true
我尝试通过ssl://smtp.gmail.com更改主机,将端口更改为465(仅为尝试不同的内容),但它仍然无法工作。无论如何,如果原始参数在Eclipse中正常工作,我想值是正确的,但问题是创建jar时出现了问题。我不太了解创建jar时可能产生的可能结果或更改。当创建jar时,JavaMail库可能会出错吗?
你有任何想法吗?