wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Java Mail send message to Yahoo account Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Java Mail send message to Yahoo account" Watch "Java Mail send message to Yahoo account" New topic

Java Mail send message to Yahoo account

Nancy Johnson

Joined: Jan 30, 2003
Posts: 2
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>");
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");
C:\>java TestYahoo pop.mail.yahoo.com smtp.mail.yahoo.com Nancy_Johnson2002 [xx my password] Nancy_johnson2002@yahoo.com Nancy_Johnson2002@yahoo.com
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...

Joined: Jan 20, 2002
Posts: 17
Dear Nancy Johnson,
yahoo doesn't allow to use their smtp or pop mail addresss in free of cost. so u will have to change the both of these two addresses.
n u can send mail by using the following code -
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class mailer
public static void main (String args[]) throws Exception
String smtpHost = "smtp.id.eth.net";
String from= "rajrastogi2k@yahoo.com";
String to= "rajrastogi2k@yahoo.com";
Properties props = System.getProperties();
props.put( "mail.smtp.host", smtpHost );
Session session = Session.getDefaultInstance( props, null );
session.setDebug( true );
MimeMessage message = new MimeMessage( session );
message.setFrom( new InternetAddress(from) );
message.addRecipient( Message.RecipientType.TO, new InternetAddress(to) );
message.setSubject( "subject" );
message.setContent( "message", "text/html" );
Transport.send( message );
If it doesn't work, then change the smtp address n run this program.
Nancy Johnson

Joined: Jan 30, 2003
Posts: 2
Thanks for the help. I have decided to use the company's smtp server instead. It took me a while to figure out the smtp server name of my company since it's very hard to get a hold on mail server admin personnel. But i finally get it to work. However, i still have some problem on reading mail. The smtp server i am using allow me to send mail to any account. But when i try to read the mail, it doesn't seem to work. Why do i need to use pop3 as protocol? Does send and receive mail use different mail server? The server name i used for sending mail doesn't seem to be right for retrieving mails. I keep on getting connection refused error.
Thanks again for the help, and if you have any input on my question it will be greatly appreciated.
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
I am also getting an error:

javax.mail.MessagingException: * BYE Connection refused;
nested exception is:
com.sun.mail.iap.ConnectionException: * BYE Connection refused
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:569)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)

If you got your issue resolved please let me know as well.
It seems that the problem is security level on the MS Exchange server, and BTW< you need to use IMAP instead of POP3, since, MOST MS EXchange server, disable POP3 (these are internal corporate e-mail exchange servers)
Vivek Mathur

Joined: Jun 29, 2007
Posts: 25
Hi Nancy and kamal.

You are getting the problem when smtp server refuses to connect.
You can try out one thing, just set connectiontimeout property of server.

props.put("mail.smtp.connectiontimeout", "21600000" );
props.put("mail.smtp.timeout", "21600000" );

It may help you out.
[ November 28, 2007: Message edited by: Vivek Mathur ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41604
Please note that this thread is from 2003, so it's unlikely that the participants from way back then are still following it: DontWakeTheZombies

Secondly, there was no mention of Exchange, so I don't think that's relevant to this discussion.

Lastly, it's not a question a using POP or IMAP, it's a question of trying to use an SMTP server for POP3, which obviously won't work.

Ping & DNS - my free Android networking tools app
I agree. Here's the link: http://aspose.com/file-tools
subject: Java Mail send message to Yahoo account