my dog learned polymorphism*
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 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 "how to send a mail using java mail api" Watch "how to send a mail using java mail api" New topic
Author

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 = "user@some-domain.com";
String to = "user@some-domain.com";
String subject = "Hi There...";
String text = "How are you?";

Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
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));
message.setSubject(subject);
message.setText(text);

Transport.send(message);
}
}

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(SMTPTransport.java:1333)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:535)
at javax.mail.Transport.send0(Transport.java:151)
at javax.mail.Transport.send(Transport.java:80)
at Test.main(Test.java:28)


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


Santosh Kimothi,
Java programmer
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You need to use SSL, and you need to use authentication. See here: http://java.sun.com/products/javamail/FAQ.html#gmail


Ping & DNS - my free Android networking tools app
Keshav Prasad gurukul
Greenhorn

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


SCJP5 94% SCWCD5 86% SCBCD5 86%
 
Don't get me started about those stupid light bulbs.
 
subject: how to send a mail using java mail api