当前位置: 技术问答>java相关
如何在JSP中使用JAVAMAIL发送需要身份验证的信件???
来源: 互联网 发布时间:2015-07-27
本文导语: 为了身份验证修改 props.put("mail.smtp.auth", "true"); 增加: Transport transport = mailsession.getTransport("smtp"); transport.connect(host, username, password); 但是结果出错 javax.mail.SendFailedException: Sending failed; nested exception is: java...
为了身份验证修改
props.put("mail.smtp.auth", "true");
增加:
Transport transport = mailsession.getTransport("smtp");
transport.connect(host, username, password);
但是结果出错
javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.AuthenticationFailedException
哪位高手搞过?帮帮忙,再好贴个成功的例子!
|
下面的程序是在163上试验通过的
import java.io.*;
import java.util.*;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
*
*/
public class Mail{
public boolean sendMail(String subject,String body,String to){
try {
SmtpAuth sa=new SmtpAuth(); // 密码验证
sa.getuserinfo(你的用户名,口令);
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);//调试信息
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(你的EMAIL地址,显示名字));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args){
Mail m=new Mail();
m.sendMail("test","test javamail api",目标EMAIL地址);
}
}
这是认证函数:
import java.util.Date;
import java.util.*;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
*
*/
public class SmtpAuth extends javax.mail.Authenticator{ //SMTP身份验证
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
import java.io.*;
import java.util.*;
import java.util.Date;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
Title: Email
*
Description: 发送Email
*/
public class Mail{
public boolean sendMail(String subject,String body,String to){
try {
SmtpAuth sa=new SmtpAuth(); // 密码验证
sa.getuserinfo(你的用户名,口令);
Properties props=System.getProperties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
Session sess=Session.getInstance(props,sa);
sess.setDebug(true);//调试信息
Message msg = new MimeMessage(sess);
msg.setDataHandler(new DataHandler(body,"text/html; charset=gb2312"));
msg.setFrom(new InternetAddress(你的EMAIL地址,显示名字));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(body);
Transport.send(msg);
return true;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args){
Mail m=new Mail();
m.sendMail("test","test javamail api",目标EMAIL地址);
}
}
这是认证函数:
import java.util.Date;
import java.util.*;
import java.util.Hashtable;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
Title: Email身份验证
*
Description: Email身份验证
*/
public class SmtpAuth extends javax.mail.Authenticator{ //SMTP身份验证
private String user,password;
public void getuserinfo(String getuser,String getpassword){
user=getuser;
password=getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
|
下面是我写的程序,成功了,你看看吧。
发送确认信
您的第一步注册已经完成,下面进行第二步注册:
我们已经向你的邮箱发送了一封确认信,请点击信中的确认链接以激活账户。
发送确认信
您的第一步注册已经完成,下面进行第二步注册:
我们已经向你的邮箱发送了一封确认信,请点击信中的确认链接以激活账户。