aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending an email using Javamail" Watch "Sending an email using Javamail" New topic
Author

Sending an email using Javamail

Padma Priya
Ranch Hand

Joined: Feb 01, 2007
Posts: 113
Hi

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");
props.put("mail.smtp.port","25");


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

// 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");
msg.setFrom(addressFrom);

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.setSubject(subject);
//msg.setContent(message, "text/plain");
//Transport.send(msg);


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

transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));

transport.close();


}
}


I get th following error message:

Exception in thread "main" javax.mail.NoSuchProviderException: Invalid protocol:
null
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
Deepthi
Paul Sturrock
Bartender

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: 113
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
1)
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

thanks
Deepthi
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Sending an email using Javamail