我做的是用户找回密码的功能 当我引入SendMail.java就出错java.lang.reflect.InvocationTargetException是怎么回事
actionSendMail send = new SendMail();
int count=send.sand(userVO.getEmail(),"密码找回",content,path);
/*
* Sendmain.java
* 2007.6.19:15:07 @ in pnetp.com
*/
package com.util;import java.util.Date;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public class SendMail{
//Smtp服务IP;
private String host = null;
//发送者邮箱;
private String from = null;
//接收者邮箱;
private String to = null;
//本地附件;
private String fileAttachment = null;
//邮件主题;
private String subject = null;
//邮件内容;
private String text = null;
public String getFileAttachment() {
return fileAttachment;
}
public void setFileAttachment(String fileAttachment) {
this.fileAttachment = fileAttachment;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public boolean sendM(){
try{
// system properties
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.timeout","25000");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
// 获取 session
Session sendMailSession = Session.getInstance(props,new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "sinew2009"); } }); // 声名 message
MimeMessage message = new MimeMessage(sendMailSession);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
// 建立 message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //内容;
messageBodyPart.setText(text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// 附件;
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送邮件;
Transport.send(message);
return true;
}catch(MessagingException m){
m.printStackTrace();
return false;
}
}
public int sand(String to,String title,String text,String fujian){
SendMail sm = new SendMail();
sm.setFileAttachment(fujian); //本地附件;
sm.setFrom("[email protected]"); //发送者邮箱;
sm.setTo(to); //接收者邮箱;
sm.setHost("smtp.gmail.com"); //Smtp服务IP;
sm.setSubject(title); //邮件主题
sm.setText(text); //邮件内容
int i=-1;
if(sm.sendM()){
i=1;
}else{
i=2;
}
return i;
}
}
actionSendMail send = new SendMail();
int count=send.sand(userVO.getEmail(),"密码找回",content,path);
/*
* Sendmain.java
* 2007.6.19:15:07 @ in pnetp.com
*/
package com.util;import java.util.Date;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;public class SendMail{
//Smtp服务IP;
private String host = null;
//发送者邮箱;
private String from = null;
//接收者邮箱;
private String to = null;
//本地附件;
private String fileAttachment = null;
//邮件主题;
private String subject = null;
//邮件内容;
private String text = null;
public String getFileAttachment() {
return fileAttachment;
}
public void setFileAttachment(String fileAttachment) {
this.fileAttachment = fileAttachment;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public boolean sendM(){
try{
// system properties
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.timeout","25000");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
// 获取 session
Session sendMailSession = Session.getInstance(props,new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "sinew2009"); } }); // 声名 message
MimeMessage message = new MimeMessage(sendMailSession);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setSentDate(new Date());
// 建立 message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //内容;
messageBodyPart.setText(text);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// 附件;
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送邮件;
Transport.send(message);
return true;
}catch(MessagingException m){
m.printStackTrace();
return false;
}
}
public int sand(String to,String title,String text,String fujian){
SendMail sm = new SendMail();
sm.setFileAttachment(fujian); //本地附件;
sm.setFrom("[email protected]"); //发送者邮箱;
sm.setTo(to); //接收者邮箱;
sm.setHost("smtp.gmail.com"); //Smtp服务IP;
sm.setSubject(title); //邮件主题
sm.setText(text); //邮件内容
int i=-1;
if(sm.sendM()){
i=1;
}else{
i=2;
}
return i;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货