Teresa 2006-4-18 14:21
求助!!!
为什么 sendTo 的电子邮件址址用自己本机的地址就行也可以发送到本机,但如果用一般的电子邮件地址如: [email]xxx@xxx.com[/email] ,就出现如下错误:
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class SendMail {
private String sendTo = "my.goto.net";//本机的,测试其他的是用[email]xxx@xxx.com[/email]
private String sendFrom = "[email]my@my.com[/email]";
private String subject = "Hello,This is a email";
public SendMail() {
}
public void send(){
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "my.goto.net");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(this.sendFrom));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(this.sendTo));
newMessage.setSubject(this.subject);
newMessage.setSentDate(new Date());
newMessage.setText("this is content,succeed.");
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
}catch(AddressException e){
System.out.println("-----------address exception-----------");
e.printStackTrace();
}catch(MessagingException e){
System.out.println("-----------message exception-----------");
e.printStackTrace();
}
}
public static void main(String args[]){
SendMail s = new SendMail();
System.out.println("----------------- start ------------------");
s.send();
System.out.println("----------------- succeed ------------------");
}
}
为什么 sendTo 的电子邮件址址用自己本机的地址就行也可以发送到本机,但如果用一般的电子邮件地址如: [email]xxx@xxx.com[/email] ,就出现如下错误:
----------------- start ------------------
-----------message exception-----------
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
javax.mail.SendFailedException: 550 5.7.1 Unable to relay for [email]xxx@xxx.com[/email]
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at mail.SendMail.send(SendMail.java:41)----------------- succeed ------------------
at mail.SendMail.main(SendMail.java:54)
是怎么回事?
悠着点 2006-4-18 14:22
// java mail package [url]http://java.sun.com/products/javamail/index.html[/url]
//java mail depends on [url]http://java.sun.com/products/javabeans/glasgow/jaf.html[/url]
//Set your CLASSPATH to include the "mail.jar" file obtained from
// the download, as well as the current directory.
// Assuming you unzipped javamail-1_2.zip in c:\download the
// following would work:
// set CLASSPATH=%CLASSPATH%;c:\download\javamail-1.2\mail.jar;.
// Also include the "activation.jar" file that you obtained from
// downloading the JavaBeans Activation Framework, in your CLASSPATH.
// set CLASSPATH=%CLASSPATH%;c:\download\activation\activation.jar
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mail extends JFrame
{
JLabel jlSendMail = new JLabel("Send Mail");
public Mail()
{
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(jlSendMail);
jlSendMail.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
try{
String host = "smtp.21cn.com";//"Mail Server in your Company";
String from = "[email]aaaa@21cn.com[/email]";//"From Email";
String to = "[email]bbbb@21cn.com[/email]";//"To Email";
Properties props = new Properties();
Session session;
Transport transport;
props.put("mail.smtp.host", host);
session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from,"Ahmad"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to,"Ahmadwa"));
message.setSubject("First");
//message.setSentDate(new Date());
message.setText("Hi Emad" + (char)(10) + "How are you? This is a test mail send by java program"+new Date());
transport = session.getTransport("smtp");
transport.send(message);
System.out.println("Your Mail Send Successfully");
}
catch (MessagingException e) {System.out.println("1)" + e.toString());}
catch (UnsupportedEncodingException e) {System.out.println("2)" + e.toString());}
}
});
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String args[])
{
new Mail();
}
}
tbp 2006-4-18 14:23
java的sendmail做的都是relay的转发。如果你用别人的smtp服务器,人家不让你relay就不行了。你可以自己发给自己,说明你的程序没有什么问题。而且你本机的sendmail应给也没有什么问题,不过是打开了relay的设置吧。