我正在尝试使用软件包javax.mail.*
在Eclipse中一切正常,但是导出到Runnable Jar后,电子邮件将不会发送。您能帮我理解可能是什么问题吗?
如果使用"using javax.mail.*"
时遇到问题,是否可以使用其他库?
导出时,我使用“将所需的库打包到生成的JAR中”
谢谢。
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class sadasddas {
public static String ErrorMsg;
private static final String MAIL_SERVER = "smtp";
private static final String SMTP_HOST_NAME = "smtp.mail.com";
private static final int SMTP_HOST_PORT = 587;
private static final String USER_NAME = "someting@mail.com";
private static final String PASSWORD = "*****************";
public static void main(String[] args) {
String[] to = { "dest@mail.com" };
String subject = "Java Send Mail Attachement Example";
String body = "Welcome to Java Mail!<h1>Hello</h1>";
String[] attachFiles = new String[3];
attachFiles[0] = "C:/Til/Result/download.txt";
attachFiles[1] = "C:/Til/Result/Printer.txt";
attachFiles[2] = "C:/Til/Result/software.txt";
try {
sendEmailWithAttachments(to, subject, body, attachFiles);
System.out.println("Email Sent....!");
} catch (Exception ex) {
System.out.println("Could not send email....!");
ex.printStackTrace();
}
}
public static void sendEmailWithAttachments(String[] to, String subject, String body, String[] attachFiles) throws AddressException, MessagingException {
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", SMTP_HOST_NAME);
properties.put("mail.smtp.user", USER_NAME);
properties.put("mail.smtp.password", PASSWORD);
properties.put("mail.smtp.port", SMTP_HOST_PORT);
properties.put("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);
}
};
Session session = Session.getInstance(properties, auth);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(USER_NAME));
InternetAddress[] toAddress = new InternetAddress[to.length];
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
message.setContent(multipart);
Transport transport = session.getTransport(MAIL_SERVER);
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, USER_NAME, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Sent Message Successfully....");
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
答案 0 :(得分:0)
不仅考虑到您的问题,还考虑到您在评论中所写的内容,问题在于您试图通过双击从桌面运行 jar 文件,您可以使用 GUI 应用程序执行此操作,但不能使用控制台执行此操作应用。对于控制台应用程序,您可以创建一个 .bat 文件来启动 cmd 并在 cmd 中运行 jar 文件。然后您可以在桌面上创建 .bat 文件的快捷方式并双击运行它。
.bat 文件应如下所示:
echo off
start cmd.exe /c
cd /d "d:\path_to_the_jar_file\"
java -jar "My Program.jar"
pause
echo on