Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending an email using Javamail

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic