|
I wrote a small class for sending emails according to the book, but I can't always send it out. Please help me check it. There are two classes, MailSedn.java is sent, and s.java is the main class.
//MailSend.java:
import java.io. *;
import java.text. *;
import java.net. *;
import java.util.Properties;
import java.util.Date;
import javax.mail. *;
import javax.mail.internet. *;
public class MailSend {
String subject = null, text = null, mailhost = null;
public MailSend () {
mailhost = "localhost";
text = getUserText ();
subject = "Welcome ~";
}
public String sednMsg (String from, String to) {
boolean debug = false;
String err = null;
try {
Properties props = System.getProperties ();
if (mailhost! = null)
props.put ("mail.smtp.host", mailhost);
Session session = Session.getDefaultInstance (props, null);
if (debug)
session.setDebug (true);
Message msg = new MimeMessage (session);
Ranch
// from = null;
if (from! = null) {
msg.setFrom (new InternetAddress (from));
System.out.print ("from is not null ~");
}
else {
msg.setFrom ();
System.out.print ("from is null ~");
}
msg.setRecipients (Message.RecipientType.TO, InternetAddress.parse (to, false));
Ranch
msg.setSubject (subject);
msg.setText (text);
Ranch
msg.setSentDate (new Date ());
Transport.send (msg);
System.out.println ("Mail has been send successfully!");
Ranch
}
catch (Exception e) {
e.printStackTrace ();
err = e.toString ();
err = "Mail send has an error!";
}
return err;
}
Ranch
public String getUserText () {
String userText = "";
String nowDate = DateFormat.getDateInstance (). Format (new Date ());
userText = "hello ~";
return userText;
}
}
//S.java
public class S {
/ **
* @param args
* /
public static void main (String [] args) {
// TODO Auto-generated method stub
String mail = new String ("suhuajun2001@163.com");
MailSend ms = new MailSend ();
ms.sednMsg ("shj0717@gmail.com", "suhuajun2001@163.com");
//ms.sednMsg ("", mail);
}
}
The error message is as follows:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com
at com.sun.mail.smtp.SMTPTransport.rcptTo (SMTPTransport.java:1196)
at com.sun.mail.smtp.SMTPTransport.sendMessage (SMTPTransport.java:584)
at javax.mail.Transport.send0 (Transport.java:169)
at javax.mail.Transport.send (Transport.java:98)
at server.MailSend.sednMsg (MailSend.java:47)
at server.S.main (S.java:12)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for suhuajun2001@163.com
Let's see what is the reason. Also, I have included the jar files (mail.java, activation.java) to be used ~~~ |
|