环境是在公司的内网,在网内发给任何人都是可以的,但当发给外网的地址时老是有错,如[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.
我们过去是用一个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.
解决方案 »
- 重新改了还是出错
- 在Eclipse里对List数组运用泛型,出现"Type Safety"警告。
- 关于java接收完用户输入后强行结束程序提示自动关机的问题
- 怎样把java程序放在没有安装JRE的电脑上运行?
- JDK路径设置全解 遇到过 java.lang.NoClassDefFoundError 的Java新手请进
- 关于java的telnet程序受信方面的问题!快点救救我呀!可以再加100分!
- 正则表达式的问题,求救!!
- JComboBox 中事件侦听
- 求数字签名源代码示例
- 谁能给我一个java作图的例子啊!很急用啊!只要能做简单的图形就可以了!!
- panle绘图与加组件问题
- 帮忙解释一下接口的耦合,完全耦合,接藕,看书不大懂~
改为
String host = "你公司的smtp server地址";
如果mail server需要验证,则程序里必须验证。
我感觉一般的邮件服务器都需要密码验证,否则这个邮件服务器还不成了垃圾邮件的集中营了?
我是这么感觉的
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
2)用transport.sendMessage()去代替transport.send()
具体请参考JavaMail API官网:http://www.oracle.com/technetwork/java/faq-135477.html