当前位置: 代码迷 >> Java Web开发 >> 用JAVA程序进行邮件发送,出现一个异常,求解决方案?
  详细解决方案

用JAVA程序进行邮件发送,出现一个异常,求解决方案?

热度:9705   发布时间:2013-02-25 21:12:33.0
用JAVA程序进行邮件发送,出现一个错误,求解决方案???在线等!!!
本机与本机之间发送,可以正常发送。但是本机给126邮箱发送 则报错误!求解释,错误代码在下面!![size=18px][/size]
属性文件:mail.properties
#邮件发送服务器地址
mail.smtp.host=smtp.126.com
#是否需要安全验证
mail.smtp.auth=ture
#邮件接收服务器地址
mail.pop.host=smtp.126.com
mail.uesr=admin@tjise.com
mail.password=admin


程序代码:
public class MAILUtils {
private static Properties prop=new Properties();
static{
try {
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("mail.properties"));
} catch (IOException e) {
  System.out.println("没有找到邮件服务器的参数");
e.printStackTrace();
}

}
//获取邮件服务器的Session对象
private static Session getSession(){
return Session.getInstance(prop, new MailAuthenticator(prop.getProperty("mail.user"),prop.getProperty("mail.password")));
}
//把字符串地址包装成Address对象
private static Address WraperAddress(String address){
Address ad=null;
try {
ad=new InternetAddress(address);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ad;
}
private static Address[] WraperAddress(String[] address){
int length=address==null?0:address.length;
Address[] ads=new Address[length];
for(int i=0;i<length;i++){
ads[i]=WraperAddress(address[i]);
}
return ads;
}
//根据参数创建一个HTML内容的的消息对象
private static Message createHTMLMessage(Session session,String fromAddress,String[] toAddress,String subject,String content){

Message msg=new MimeMessage(session);
//---------设置属性
//发件人
try {
msg.setFrom(WraperAddress(fromAddress));
} catch (MessagingException e) {
e.printStackTrace();
}
//收件人
try {
msg.setRecipients(Message.RecipientType.TO,WraperAddress(toAddress));
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置主题
try {
msg.setSubject(subject);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//设置内容
try {
msg.setContent(content, "text/html;charset=utf-8");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return msg;
}

//发送邮件
/**群发HTML电子邮件

* */
private static void sendHTMLEmail(String fromAddress,String[] toAddress,String subject,String content){
Session session=getSession();
Message msg=createHTMLMessage(session, fromAddress,toAddress,subject,content);
try {
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}

 public static void main(String[] args){
// String[] to={"test@tjise.com"};
String[] to={"m18722167404@163.com"};
sendHTMLEmail("admin@tjise.com", to, "JavaMail测试5","本邮件为一个测试邮件2");
 
 }
 
 
}
//网络连接身份验证
class MailAuthenticator extends Authenticator{
  private String username;
  private String password;
   
public MailAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}

@Override
protected PasswordAuthentication getPasswordAuthentication() {
   
return new PasswordAuthentication(username, password);
}


}

错误信息:


com.sun.mail.smtp.SMTPSendFailedException: 553 Requested action not taken: Local user only,smtp4,jdKowEC5uEMV6jBQWMZmAg--.295S2 1345382934

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
  相关解决方案