File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes how to send a mail using java mail api 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 "how to send a mail using java mail api" Watch "how to send a mail using java mail api" New topic

how to send a mail using java mail api

santosh kimothi
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.util.Properties;

public class Test {
public static void main(String[] args) throws AddressException, MessagingException {
String from = "";
String to = "";
String subject = "Hi There...";
String text = "How are you?";

Properties properties = new Properties();
properties.put("", "");
properties.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(properties, null);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));


i am trying to send a mail with the above program but i am getting the following error message............

Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 21sm775431pxi.0

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(
at com.sun.mail.smtp.SMTPTransport.mailFrom(
at com.sun.mail.smtp.SMTPTransport.sendMessage(
at javax.mail.Transport.send0(
at javax.mail.Transport.send(
at Test.main(

can anybody help me .............. its urgently required .....please

Santosh Kimothi,
Java programmer
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
You need to use SSL, and you need to use authentication. See here:
Keshav Prasad gurukul

Joined: Feb 25, 2009
Posts: 14
Thanks Ulf Dittmer.

SCJP5 94% SCWCD5 86% SCBCD5 86%
I agree. Here's the link:
subject: how to send a mail using java mail api
It's not a secret anymore!