javaweb 发送邮件 异常解决、 --在线等
抛出异常:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 Mailbox not found
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1873)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at org.nfxy.servlet.SendMail.main(SendMail.java:47)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 Mailbox not found
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1724)
... 4 more
代码:
public class SendMail extends HttpServlet {
/**
*
*/
private static MailAuthenticator autherticator=null;
public static void main(String[] args) {
for (int i=0;i<5;i++){
String from="546206899@qq.com";
String to="2856493102@qq.com";
String smtpServer="smtp.qq.com";
String subject="Hello ,this is a email Test!!";
String content ="Welcome to you!!";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
autherticator = new MailAuthenticator("546206899@qq.com","xxxxxxxxx");
Session session = Session.getDefaultInstance(props,autherticator);
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
System.out.println("成功发送邮件......");
}catch(Exception se){
se.printStackTrace();
}
}
}
}
//现在的大部分的邮件服务器都要求有身份验证,所以需要此类实现验证功能
class MailAuthenticator extends Authenticator{
private String username = null;
private String userpasswd = null;
public MailAuthenticator(){}
public MailAuthenticator(String username,String userpasswd){
this.username = username;
this.userpasswd = userpasswd;
}
public void setUserName(String username){
this.username = username;
}
public void setPassword(String password){
this.userpasswd = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,userpasswd);
}
}
------解决方案--------------------
qq邮箱中,smtp等服务是否启用。
------解决方案--------------------
你的代码没有问题,可以正常发送邮件
可能是你QQ邮件的设置问题吧,请尝试换网易或者其他的邮箱试试
------解决方案--------------------
SMTP配置有误!
很多方面,有可能是你用局域网,有可能是加载的JAR包原因,还有就是QQ邮箱内没开启SMTP服务!