环境是在公司的内网,在网内发给任何人都是可以的,但当发给外网的地址时老是有错,如[email protected],请问有人做过不需要验证而且要能发到外网的程序吗,是不是必须要验证啊,我总不能把自己的用户名和密码写到配置文件里吧.
我们过去是用一个ASP的组件发的,换到JAVAMAIL,怎么就不行了?public static void main(String[] args) {  
try{  
      String[] to = {"[email protected]","[email protected]"};
      String from = "[email protected]";
      String host = "mailserver";
      String subject = "this is a test";
      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", 25);
      Session session;
      session = Session.getDefaultInstance(props, null);
      session.setDebug(true);
      MimeMessage msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(from));      this.address = new InternetAddress[to.length];
      for (int i = 0; i < to.length; ++i) {
        this.address[i] = new InternetAddress(to[i]);
      }      msg.setRecipients(Message.RecipientType.TO,address);
      msg.setSubject("this is a test");
      Transport.send(msg);
    }
    catch (MessagingException mex)
    {
    }
}
错误代码如下
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Resetting
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for [email protected] at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1294)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:635)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at sendmail_autoftp.sendmail(sendmail.java:60)
at sendmail_autoftp.main(sendmail.java:74)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for [email protected] at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1145)
... 5 more
QUIT
221 Closing connection. Good bye.

解决方案 »

  1.   

    String host = "mailserver"; 
    改为
    String host = "你公司的smtp server地址";
      

  2.   

    需不需要密码验证应该是接收邮件的mail server的设置吧?
    如果mail server需要验证,则程序里必须验证。
    我感觉一般的邮件服务器都需要密码验证,否则这个邮件服务器还不成了垃圾邮件的集中营了?
    我是这么感觉的
      

  3.   

    谢谢各位了,你们说的都是需要用户名和密码,我上面的程序不需要验证已经能发信,只是不能发到别的domain, 只能发给公司的人,我希望能发到外网去,可能从安全上来讲是不允许的
      

  4.   

    可能你公司的smtp server block了.
      

  5.   

    Q: What's the difference between the Transport methods send and sendMessage? 
    A: The send() method is a static method and can be used without needing an instance of a Transport object. It is intended for the common, simple case of sending a single message using the default transport. Internally, the send() method will first call the saveChanges() method on the message. It will then create an appropriate new Transport object, call the Transport's connect() method, call the Transport's sendMessage() method to actually send the message, call the Transport's close() method, and finally abandon the new instance of the Transport object to be collected by the garbage collector. (Actually, it's rather more complicated than that, but that's the general idea.) As you can see, the static send() convenience method is built on the more general per-instance sendMessage() method. There are a number of reasons for an application to use the sendMessage() method directly. The most common reasons are to improve performance by sending more than one message during a single connection, or to manually manage the connection so as to provide authentication information. Q: When I try to send a message I get an error like SMTPSendFailedException: 530, Address requires authentication. 
    A: You need to authenticate to your SMTP server. The package javadocs for the com.sun.mail.smtp package describe several methods to do this. The easiest is often to replace the call Transport.send(msg); with     String protocol = "smtp";
        props.put("mail." + protocol + ".auth", "true");
        ...
        Transport t = session.getTransport(protocol);
        try {
            t.connect(username, password);
            t.sendMessage(msg, msg.getAllRecipients());
        } finally {
            t.close();
        }
     You'll have to supply the appropriate username and password needed by your mail server. Note that you can change the protocol to "smtps" to make a secure connection over SSL. 
    Q: I need to authenticate to my SMTP server so I call trans.connect(host, user, password) and then trans.send(msg) to send the message, but it's not working. 
    A: You should call trans.sendMessage(msg, addrs) to send the message. As described above, the send method is a static convenience method that acquires its own Transport object and creates its own connection to use for sending; it does not use the connection associated with any Transport object through which it is invoked. 
    http://www.oracle.com/technetwork/java/faq-135477.html#send
      

  6.   

    1)用 props.put("mail.smtp.auth", "true"); 
    2)用transport.sendMessage()去代替transport.send()
    具体请参考JavaMail API官网:http://www.oracle.com/technetwork/java/faq-135477.html