import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
private static Properties props = null;
static {
props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "smtp.sina.com");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.debug","false");
}
if ("email".equals(type)) {
try {
//根据eamil查询是注册过
clientUser = clientUserDao.getByEmail(phone);
} catch (SQLException e) {
e.printStackTrace();
this.printResult(ResultCode.CODE_600);
return ActionSupport.SUCCESS;
}
//如果查到clientUser,就发送邮件
if(clientUser!=null){
String findPwdLink = generateResetPwdLink(clientUser);
clientUser.setFindPwdLink(findPwdLink);
try {
//把地址存到clientUser中
clientUserDao.update(clientUser);
} catch (SQLException e) {
e.printStackTrace();
this.printResult(ResultCode.CODE_600);
return ActionSupport.SUCCESS;
}
// 创建Session实例对象
Session session = Session.getDefaultInstance(props);
// 创建MimeMessage实例对象
MimeMessage message = new MimeMessage(session);
// 设置发件人
try {
message.setFrom(new InternetAddress("wuhao920508@sina.com"));
// 设置邮件主题
message.setSubject("找回密码");
// 设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(phone));
// 设置发送时间
message.setSentDate(new Date());
// 设置纯文本内容为邮件正文
message.setText("要使用新的密码, 请使用以下链接启用密码:<br/><a href='" + findPwdLink
+ "'>点击重新设置密码</a>");
// 保存并生成最终的邮件内容
message.saveChanges();
// 获得Transport实例对象
Transport transport = session.getTransport();
// 打开连接
transport.connect("wuhao920508@sina.com", "******");
// 将message对象传递给transport对象,将邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
// 关闭连接
transport.close();
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
不知道为什么,用debug一步一步调也没有错误,也运行到最后,就是不发邮件
求教啊,各位大神,我也下载activation.jar和mail.jar都放lib下了,但是感觉没有到这两个jar
findPwdLink这是我用MD5加密的地址信息
我之前也遇到这种情况,如果你是发送成功,但收不到,可能是收件邮箱把你发送的邮件当垃圾邮件处理未显示出来,你到垃圾邮件那找下看有没有。