This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 41068
You need to use SSL, and you need to use authentication. See here:

Ping & DNS - my free Android networking tools app
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
Similar Threads
Sending Email in java through MS Exchange Server
Give me solution to send mail
Javamail error
JavaMail : "Access not allowed"
Sending mail with authentication to Exchange Server 2007