jQuery in Action, 3rd edition
The moose likes Other JSE/JEE APIs and the fly likes Sending an email using Javamail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending an email using Javamail" Watch "Sending an email using Javamail" New topic

Sending an email using Javamail

Padma Priya
Ranch Hand

Joined: Feb 01, 2007
Posts: 116

I am new to javaMail....And i having a problem with this program.
I getting the following exception when the run the following program

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;

public class MailDemo
public static void main(String[] args) throws Exception
boolean debug = false;

//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mail.yahoo.com");

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);

// create a message
Message msg = new MimeMessage(session);

Transport transport = session.getTransport();

// set the from and to address
InternetAddress addressFrom = new InternetAddress("sheshachala5@yahoo.com");

InternetAddress addressTo = new InternetAddress("nebula_228@yahoo.com");

msg.setRecipient(Message.RecipientType.TO, addressTo);

// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");

// Setting the Subject and Content Type

//msg.setContent(message, "text/plain");

msg.setSubject("Testing javamail plain");
msg.setContent("This is a test", "text/plain");




I get th following error message:

Exception in thread "main" javax.mail.NoSuchProviderException: Invalid protocol:
at javax.mail.Session.getProvider(Session.java:431)
at javax.mail.Session.getTransport(Session.java:650)
at javax.mail.Session.getTransport(Session.java:631)
at javax.mail.Session.getTransport(Session.java:617)
at MailDemo.main(MailDemo.java:25)

It is a simple program to send a plain email...
Can anyone help me out

Thanks in advance
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Looks like you probably haven't set the mail.transport.protocol property. You need to tell JavaMail which protocol you are using to send emails so it knows which Transport to use. If you add:

you should get rid of that particular error.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Padma Priya
Ranch Hand

Joined: Feb 01, 2007
Posts: 116
hi Paul,

When i made the following correction...now i am getting a different error

Exception in thread "main" java.lang.IllegalStateException: Not connected
at com.sun.mail.smtp.SMTPTransport.checkConnected(SMTPTransport.java:151
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:548)
at MailDemo.main(MailDemo.java:50)

Could you please help me out with this one

Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
You may want to start out by checking the requirements of Yahoo. According to this page:

Client Settings: Outgoing (SMTP) Server: smtp.mail.yahoo.com (Use SSL, port: 465, use authentication)

Instead of trying to use Yahoo's account, why not try to use your ISP provider first and see if you can get that working? Also, the stack trace is showing the error at line 50 of the MailDemo. Which line is that in your code? That will help you pin down the problem.
I agree. Here's the link: http://aspose.com/file-tools
subject: Sending an email using Javamail
jQuery in Action, 3rd edition