您好我想使用下面的代码发送邮件,如果它被执行,它将抛出一个异常给出下面我需要做什么..我正在使用java,eclipse,我已经添加了javaMailAPI jars也....做我需要额外的罐装这个程序。
package firstpackage;
/*public class SendMail { }*/
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.exception.ExceptionUtils;
public class SendMail {
public SendMail(String fromMail, String tomail)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("Email_Id","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromMail));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(tomail));
message.setSubject("Script failed");
//message.setText("your test has failed for script name:Name of your scipt <============================>"+ ExceptionUtils.getStackTrace(e) );
Transport.send(message);
System.out.println("Done");
} catch (MessagingException ex) {
throw new RuntimeException(ex);
}
}
public static void main(String args[])
{
new SendMail("memusipra4@gmail.com","spmukalla@gmail.com");
}
}
,例外是:
Exception in thread "main" java.lang.RuntimeException: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
at firstpackage.SendMail.<init>(SendMail.java:42)
at firstpackage.SendMail.main(SendMail.java:48)
Caused by: javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
at javax.mail.Session.getProvider(Session.java:237)
at javax.mail.Session.getTransport(Session.java:346)
at javax.mail.Session.getTransport(Session.java:376)
at javax.mail.Transport.send(Transport.java:67)
at javax.mail.Transport.send(Transport.java:48)
at firstpackage.SendMail.<init>(SendMail.java:39)
... 1 more
答案 0 :(得分:1)
首先,您需要修复这些common mistakes,尽管它们不是您问题的根源。
您是否按照these instructions将JavaMail添加到项目中?
尝试在Eclipse之外运行程序,例如,将应用程序打包在jar文件中,然后使用“java -cp SendMail.jar:javax.mail.jar firstpackage.SendMail”。