异常: javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay代码:import java.util.Properties;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import com.sun.mail.smtp.SMTPMessage;public class mailsender {
      
protected ParametersManager pm = ParametersManager.getInstance(); public void sendToUser(String email, String content) {
System.out.println("sendToUer:"+pm); Properties mailProp = new Properties();
         
try { mailProp.setProperty("mail.smtp.host", pm.getItem("mail.user.host"));

mailProp.setProperty("mail.smtp.port", pm.getItem("mail.user.port"));
          
Session session = Session.getDefaultInstance(mailProp);
               
SMTPMessage mail = new SMTPMessage(session);
            
InternetAddress from = new InternetAddress(pm .getItem("mail.user.sender")); mail.setFrom(from); InternetAddress[] receiver = new InternetAddress[1]; receiver[0] = new InternetAddress(email); mail.setRecipients(javax.mail.Message.RecipientType.TO, receiver); mail.setSubject(pm.getItem("mail.user.subject")); mail.setContent(content, "text/html;charset=UTF-8"); Transport.send(mail); } catch (MessagingException me) { System.out.println(me.toString()); me.printStackTrace(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); } } public void sendToProductOwner(String content, String sender) { Properties mailProp = new Properties();
try {
mailProp.setProperty("mail.smtp.host", pm .getItem("mail.owner.host")); mailProp.setProperty("mail.smtp.port", pm .getItem("mail.owner.port"));
mailProp.setProperty("mail.smtp.auth", "false"); Session session = Session.getDefaultInstance(mailProp); SMTPMessage mail = new SMTPMessage(session); InternetAddress from = null; if (sender == null) { from = new InternetAddress(pm.getItem("mail.owner.sender")); } else { from = new InternetAddress(sender); } String[] rece = pm.getItem("mail.owner.receiver").split(","); InternetAddress[] to = new InternetAddress[rece.length]; for (int i = 0; i < rece.length; i++) { to[i] = new InternetAddress((String) rece[i]); } mail.setFrom(from); mail.setRecipients(javax.mail.Message.RecipientType.TO, to); mail.setSubject(pm.getItem("mail.owner.subject")); mail.setContent(content, "text/html;charset=UTF-8"); Transport.send(mail); } catch (MessagingException me) { System.out.println(me.toString()); me.printStackTrace(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); } } public static void main(String s[]) { mailsender ms = new mailsender();// ms.sendToUser("[email protected]", "xxxxx");
ms.sendToProductOwner("hello", null); ms = null; }}
package jmail;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URL;import java.net.URLDecoder;import java.util.Properties;public class ParametersManager { private static String PFILE = null; private File file = null; private long lastModifiedTime = 0; private Properties props = null; private static ParametersManager instance = new ParametersManager(); private ParametersManager() { URL url = this.getClass().getResource("config.properties"); if (url != null) { try { String path = URLDecoder.decode(url.getPath(), "UTF-8"); /*
 * 
 * For Unix FileSystem is file:/path For Windows FileSystem is
 * 
 * file:/X:/path or file:/d:/path
 */ if ((path.length() >= 3) && (path.charAt(0) == '/') && (path.charAt(2) == ':')) { path = path.substring(1); } PFILE = path.replace('/', File.separatorChar); } catch (UnsupportedEncodingException ex) { System.out.println(ex.toString()); } } file = new File(PFILE); lastModifiedTime = file.lastModified(); if (lastModifiedTime == 0) { System.out.println(PFILE + " File does not exist!"); } props = new Properties(); try { props.load(new FileInputStream(PFILE)); } catch (FileNotFoundException ex) { System.out.println(ex.toString()); } catch (IOException e) { System.out.println(e.toString()); } } /* return signal Instance */ synchronized public static ParametersManager getInstance() { return instance; } public String getItem(String key) { long newTime = file.lastModified(); if (newTime > lastModifiedTime) { props.clear(); try { props.load(new FileInputStream(PFILE)); } catch (IOException e) { System.out.println(e.toString()); } } lastModifiedTime = newTime; return props.getProperty(key); } public void setItem(String key, String value) { String Header = "paramers of jinfonet customers self-service center"; try { props.setProperty(key, value); props.store(new FileOutputStream(PFILE), Header); } catch (IOException e) { System.out.println(e.toString()); } }}
mail.owner.port=25mail.owner.sender=xudong4005@[email protected]=10.0.16.166
#mail.owner.auth=true mail.owner.subject=Jinfonet Technical Support

解决方案 »

  1.   

    那是因为你发送的邮件地址有问题或者不存在此邮箱,163,126返回一个异常com.sun.mail.smtp.SMTPAddressFailedException: 550
    你重新用一个正确的邮箱名作为收件箱试试,这种异常代码没错
      

  2.   

    用SimpleEmail吧。。要下载几个jar包,网上有。SimpleEmail email = new SimpleEmail();
    email.setHostName("smtp.qq.com");
    email.setAuthentication("你的qq邮箱账号", "邮箱密码");
    email.setCharset("utf-8");
    email.addTo("接受者的邮箱");
    email.setFrom("你的qq邮箱账号");
    email.setSubject("标题");
    email.setMsg("内容");
      

  3.   

    需要验证,from 邮件地址加账号与密码