我正在使用javax.mail-1.6.1.jar从我的gmail帐户发送电子邮件。
当我在eclipse上运行程序时,它运行良好,但是当我将程序作为可执行jar运行时,它在该行上停止-MimeMessage message = new MimeMessage(session);
没有异常。
public void sendEmail(String from, String to,String subject, String body,
String attachmentName){
try {
Session session = Session.getDefaultInstance(fMailServerConfig, new SMTPAuthenticator());
Log.getInstance(Emailer.class).getLogger().error("getProperty == "+fMailServerConfig.getProperty("mail.smtp.host"));
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
// Set the email msg text.
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(body);
// Set the email attachment file
FileDataSource fileDataSource = new FileDataSource(attachmentName);
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(fileDataSource.getName());
// Create Multipart E-Mail.
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
// Send the msg. Don't forget to set the username and password
// to authenticate to the mail server.
Transport.send(message);
} catch (Exception e) {
Log.getInstance(Emailer.class).getLogger().error("Cannot send email."+ e);
System.err.println("Cannot send email. " + e);
}
}