Hello,
I am new to the
Java mail API, and would like to use my local SMTP server to send mail to Yahoo account. I have tried varies options but none works. I do have a valid account in Yahoo, and tried to use the same account for both FROM and TO field. Also, i have configure the local SMTP server to relay mail.
The followings are the source code I got from one of the web discuss. but couldn't run it successfully
------------------------------------------------------
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class TestYahoo
{
// How to Run this program :
// java TestYahoo pop.mail.yahoo.com smtp.mail.yahoo.com <u'r yahoo username> <u'r yahoo password> <from mail address> <to mail address>
public static void main (
String args[]) throws Exception
{
if (args.length != 6)
{
System.err.println("Usage : java TestYahoo <pop-Hostname> <smtp-Hostname> <username> <password> <fromAddress> <toAddress>");
return;
}
String pop3Host = args[0];
String smtpHost = args[1];
String username = args[2];
String password = args[3];
String from = args[4];
String to = args[5];
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
Store store = session.getStore("pop3");
store.connect(pop3Host, username, password);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
Transport.send(message);
store.close();
}
}
--------------------------------------------------
C:\>java TestYahoo pop.mail.yahoo.com smtp.mail.yahoo.com Nancy_Johnson2002 [xx my password]
[email protected] [email protected] Exception in
thread "main" javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:106)
at javax.mail.Service.connect(Service.java:234)
at javax.mail.Service.connect(Service.java:135)
at TestYahoo.main(TestYahoo.java:30)
Could anyone tell me what it fail...?
Help will be greatly appreciated...
Nancy