Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to send mail to Yahoo, Gmail, Rediffmail etc using JavaMail

843830Dec 14 2005 — edited Jul 19 2010
hi,

i want to know how can we send mails to yahoo id's, Gmail id's, Rediffmail id's etc using Java Mail


here is the sample code iam posting

kinldy suggest me with u r replies

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail
{
public static void main(String[] args) throws Exception
{
String host="www.gmail.com";
String from="abc@xyz.com";
String to="helpdesk@gmail.com";
Properties props=System.getProperties();
props.put("mail.smtp.host",host);
Session session=Session.getDefaultInstance(props,null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("hi................");
message.setText("Sorry sir, This is test mail using JavaMail kindly ignore this");
try
{
Transport.send(message);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Hello World!");
}
}


Here is the error which i got when executed

javax.mail.MessagingException: Could not connect to SMTP host: www.swadesimail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
at javax.mail.Service.connect(Service.java:236)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at SendMail.main(SendMail.java:37)
Hello World!


regards,
krasr1
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 16 2010
Added on Dec 14 2005
15 comments
3,556 views