javamail发邮件出现Sending failed because of invalid destination addresses
代码如下:
request.setCharacterEncoding("GB2312");
MultipartRequest multi=new MultipartRequest(request,".",5*1024*1024,"GB2312"); //使用cos 组件
String from=multi.getParameter("from");
String to=multi.getParameter("to");
String subject=multi.getParameter("subject");
String content=multi.getParameter("content"); //附件
String fileName=multi.getFilesystemName("attachment"); //取得系统文件名
try
{
Properties pro=System.getProperties();
pro.put("mail.host","smtp.163.com");
pro.put("mail.smtp.auth","true");
Session sess=Session.getInstance(pro,new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("用户名","密码");
}
});
Message message=new MimeMessage(sess);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
if(fileName!=null) //判断文件是否存在
{
File fil=new File(fileName); //创建File对象
MimeBodyPart mbp1=new MimeBodyPart(); //mbp1代表mime类型(这里为邮件内容)的对象
mbp1.setContent(content,"text/plain;charset=gb2312"); //将邮件内容加到mbp1对象
MimeBodyPart mbp2=new MimeBodyPart(); //mbp2代表mime类型(这里为邮件附件)的对象
FileDataSource fds=new FileDataSource(fil); //创建文件数据源对象(JAF)
mbp2.setDataHandler(new DataHandler(fds));//设置数据处理类
mbp2.setFileName(MimeUtility.encodeText(fds.getName(),"gb2312","B"));
Multipart mp=new MimeMultipart(); //相当于容器,容纳BodyPart对象
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); //将mbp1和mbp2对象加入容器中
message.setContent(mp); //设置邮件消息的内容
}
else
{
message.setContent(content,"text/plain;charset=gb2312");//如果没有附件,直接处理
}
Transport trans=sess.getTransport("smtp");
trans.send(message);
out.println("it is successful!");
}
catch(MessagingException e)
{
out.println(e.toString());
}
邮件服务器为server2003 outlook下可以给内外网发邮件,也可以接收邮件,此程序可以给内网发邮件,如果给163或者qq发邮件时会出现Invalid Addresses; nested exception is: class javax.mail.SendFailedException: 550 5.7.1 Unable to relay for gcxyshiyu@qq.com 这种错误,我不知道是程序出问题了,还是邮件服务器的配置有问题,希望大家能够帮忙解决一下,非常感谢!