问题1:
发送email的时候,出现javax.mail.AuthenticationFailedException异常,
我已经 props.put("mail.smtp.auth","true")了阿,虽然出现异常,但有时候也能发送成功,有时候则失败,很不稳定。请大家给我看看代码哪里有问题
javamail.jar (1.4)
activation.jarpackage com.kysoft.common;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*; public final class sendMail
{ private boolean isDebug = false; //发送mail时是否需要输出调试信息
private Properties props; //系统属性
private Session mailSession; //邮件会话对象
private MimeMessage mimeMsg; //MIME邮件对象
private boolean needAuth = false; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private String mailContentType = null;
private String mailCharset = null;
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public sendMail()
{
props = new Properties(); //获得系统属性对象
}
public void setDebug(boolean isDebug)
{
this.isDebug = isDebug ;
}
private void setNeedAuth(boolean need)
{
if(need)
{
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
private void setSmtpHost(String hostName)
{
props.put("mail.smtp.host",hostName); //设置SMTP主机
} private boolean createMimeMessage()
{
try
{
System.out.println("准备获取邮件会话对象!");
mailSession = Session.getInstance(props); //获得邮件会话对象
mailSession.setDebug(isDebug);
}
catch(Exception e)
{
System.err.println("获取邮件会话对象时发生错误!"+e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try
{
mimeMsg = new MimeMessage(mailSession); //创建MIME邮件对象
return true;
}
catch(Exception e)
{
System.err.println("创建MIME邮件对象失败!"+e);
return false;
}
}
private void setNamePass(String name,String pass)
{
this.username = name;
this.password = pass;
} //======================================================
private boolean setFrom(String mailFrom)
{
System.out.println("设置发信人!");
if(mailFrom == null) return false;
try
{
//InternetAddress from=new InternetAddress(mailFrom);
//mimeMsg.setFrom(mailFrom);
mimeMsg.setFrom(new InternetAddress(mailFrom)); //设置发信人
return true;
}
catch(Exception e)
{
return false;
}
} private boolean setTo(String mailTo)
{
System.out.println("设置收信人!");
if(mailTo == null) return false;
try
{
InternetAddress to=new InternetAddress(mailTo);
mimeMsg.setRecipient(Message.RecipientType.TO,to);
//mimeMsg.setRecipient(Message.RecipientType.TO,new InternetAddress(mailTo));
return true;
}
catch(Exception e)
{
return false;
}
}
private boolean setSubject(String mailSubject)
{
System.out.println("设置邮件主题!");
if(mailSubject == null) return false;
try
{
mimeMsg.setSubject(mailSubject);
//System.out.println(mailSubject);
return true;
}
catch(Exception e)
{
System.err.println("设置邮件主题发生错误!");
return false;
}
}
private boolean setSentDate()
{
System.out.println("设置邮件发送日期!"); try
{
mimeMsg.setSentDate(new Date());
return true;
}
catch(Exception e)
{
System.err.println("设置邮件发送日期发生错误!");
return false;
}
}
private boolean setBody(String mailBody)
{
try
{
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(mailBody,this.mailContentType + ";charset=" + this.mailCharset);//***************
mp=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mp.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
return true;
}
catch(Exception e)
{
System.err.println("设置邮件正文时发生错误!"+e);
return false;
}
}
//邮件发送前的信息准备工作
private boolean mailSendReady()
{
try
{
this.setSmtpHost("smtp.163.com");
this.setNeedAuth(true);
if(!this.createMimeMessage()) return false;
this.username = "[email protected]"; //换成你自己测试用的163邮箱
this.password = "123456";
this.mailContentType = "text/html";
this.mailCharset = "gb2312";
if(!this.setFrom("[email protected]")) return false;
if(!this.setTo("[email protected]")) return false;
if(!this.setSubject("hello,this is a test javamail!")) return false;
if(!this.setSentDate()) return false;
if(!this.setBody("邮件发送测试!")) return false;
return true;
}
catch(Exception e)
{
System.err.println("邮件发送准备时发生错误!"+e);
return false;
}
}
public boolean sendMail()
{
try
{
if(!mailSendReady()) return false; mimeMsg.setContent(mp); //把mp作为消息对象的内容
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),username,password);
transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
}
catch(Exception e)
{
System.err.println("邮件发送失败!"+e);
return false;
}
} public static void main(String[] args)
{
sendMail themail = new sendMail();
boolean flag=themail.sendMail();
System.out.println("email sent "+flag);
}
}
//----------------------运行结果
准备获取邮件会话对象!
准备创建MIME邮件对象!
设置发信人!
设置收信人!
设置邮件主题!
设置邮件发送日期!
正在发送邮件....
邮件发送失败!javax.mail.AuthenticationFailedException
email sent false
Press any key to continue...问题2:
smtp.host/送邮箱/送账号/密码 等信息变化不大,所以我就存在数据库表中,如果在网站一运行(或者信息修改变动后)的时候,从数据库中取出,存到Properties中,这样以后再发送邮件就不需要访问数据库,直接从Properties取出,这部份代码应该怎么写才能保证每次取出的Properties都是同一个?
万分感激,问题解决后马上送分
发送email的时候,出现javax.mail.AuthenticationFailedException异常,
我已经 props.put("mail.smtp.auth","true")了阿,虽然出现异常,但有时候也能发送成功,有时候则失败,很不稳定。请大家给我看看代码哪里有问题
javamail.jar (1.4)
activation.jarpackage com.kysoft.common;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*; public final class sendMail
{ private boolean isDebug = false; //发送mail时是否需要输出调试信息
private Properties props; //系统属性
private Session mailSession; //邮件会话对象
private MimeMessage mimeMsg; //MIME邮件对象
private boolean needAuth = false; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private String mailContentType = null;
private String mailCharset = null;
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public sendMail()
{
props = new Properties(); //获得系统属性对象
}
public void setDebug(boolean isDebug)
{
this.isDebug = isDebug ;
}
private void setNeedAuth(boolean need)
{
if(need)
{
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
private void setSmtpHost(String hostName)
{
props.put("mail.smtp.host",hostName); //设置SMTP主机
} private boolean createMimeMessage()
{
try
{
System.out.println("准备获取邮件会话对象!");
mailSession = Session.getInstance(props); //获得邮件会话对象
mailSession.setDebug(isDebug);
}
catch(Exception e)
{
System.err.println("获取邮件会话对象时发生错误!"+e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try
{
mimeMsg = new MimeMessage(mailSession); //创建MIME邮件对象
return true;
}
catch(Exception e)
{
System.err.println("创建MIME邮件对象失败!"+e);
return false;
}
}
private void setNamePass(String name,String pass)
{
this.username = name;
this.password = pass;
} //======================================================
private boolean setFrom(String mailFrom)
{
System.out.println("设置发信人!");
if(mailFrom == null) return false;
try
{
//InternetAddress from=new InternetAddress(mailFrom);
//mimeMsg.setFrom(mailFrom);
mimeMsg.setFrom(new InternetAddress(mailFrom)); //设置发信人
return true;
}
catch(Exception e)
{
return false;
}
} private boolean setTo(String mailTo)
{
System.out.println("设置收信人!");
if(mailTo == null) return false;
try
{
InternetAddress to=new InternetAddress(mailTo);
mimeMsg.setRecipient(Message.RecipientType.TO,to);
//mimeMsg.setRecipient(Message.RecipientType.TO,new InternetAddress(mailTo));
return true;
}
catch(Exception e)
{
return false;
}
}
private boolean setSubject(String mailSubject)
{
System.out.println("设置邮件主题!");
if(mailSubject == null) return false;
try
{
mimeMsg.setSubject(mailSubject);
//System.out.println(mailSubject);
return true;
}
catch(Exception e)
{
System.err.println("设置邮件主题发生错误!");
return false;
}
}
private boolean setSentDate()
{
System.out.println("设置邮件发送日期!"); try
{
mimeMsg.setSentDate(new Date());
return true;
}
catch(Exception e)
{
System.err.println("设置邮件发送日期发生错误!");
return false;
}
}
private boolean setBody(String mailBody)
{
try
{
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(mailBody,this.mailContentType + ";charset=" + this.mailCharset);//***************
mp=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
mp.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
return true;
}
catch(Exception e)
{
System.err.println("设置邮件正文时发生错误!"+e);
return false;
}
}
//邮件发送前的信息准备工作
private boolean mailSendReady()
{
try
{
this.setSmtpHost("smtp.163.com");
this.setNeedAuth(true);
if(!this.createMimeMessage()) return false;
this.username = "[email protected]"; //换成你自己测试用的163邮箱
this.password = "123456";
this.mailContentType = "text/html";
this.mailCharset = "gb2312";
if(!this.setFrom("[email protected]")) return false;
if(!this.setTo("[email protected]")) return false;
if(!this.setSubject("hello,this is a test javamail!")) return false;
if(!this.setSentDate()) return false;
if(!this.setBody("邮件发送测试!")) return false;
return true;
}
catch(Exception e)
{
System.err.println("邮件发送准备时发生错误!"+e);
return false;
}
}
public boolean sendMail()
{
try
{
if(!mailSendReady()) return false; mimeMsg.setContent(mp); //把mp作为消息对象的内容
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),username,password);
transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
}
catch(Exception e)
{
System.err.println("邮件发送失败!"+e);
return false;
}
} public static void main(String[] args)
{
sendMail themail = new sendMail();
boolean flag=themail.sendMail();
System.out.println("email sent "+flag);
}
}
//----------------------运行结果
准备获取邮件会话对象!
准备创建MIME邮件对象!
设置发信人!
设置收信人!
设置邮件主题!
设置邮件发送日期!
正在发送邮件....
邮件发送失败!javax.mail.AuthenticationFailedException
email sent false
Press any key to continue...问题2:
smtp.host/送邮箱/送账号/密码 等信息变化不大,所以我就存在数据库表中,如果在网站一运行(或者信息修改变动后)的时候,从数据库中取出,存到Properties中,这样以后再发送邮件就不需要访问数据库,直接从Properties取出,这部份代码应该怎么写才能保证每次取出的Properties都是同一个?
万分感激,问题解决后马上送分
this.username = "service"
不是这个错误。