Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Properties;  
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;  
import javax.mail.internet.*;  
  
public class SendMailBySite 
{  
        public static void main(String[] args) throws UnknownHostException {  
            try {
                //InetAddress ip=InetAddress.("www.gmail.com");
                // System.out.println(ip);
                
                String host="smtp.gmail.com";
                final String user="anupvrj261@gmail.com";//change accordingly
                final String password="9452692660";//change accordingly
                String to="anupvrj261@gmail.com";//change accordingly
                String from="anupvrj261@gmail.com";
                String subject="new mad";
                String message="hii how are you";
                boolean sessionDebug=false;
                System.out.println("1");
                
                //Get the session object
                Properties props = System.getProperties();
                props.put("mail.smpt.startt.enable","true");
                
                props.put("mail.smtp.host",host);
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smpt.starttls.required","true");
              //  props.put("mail.mail.setTLS()","true");
                java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
                
                 System.out.println("2"); 
                Session mailSession = Session.getInstance(props,null);
                mailSession.setDebug(sessionDebug);
                Message msg=new MimeMessage(mailSession);
                msg.setFrom(new InternetAddress(from));
                InternetAddress[] address={new InternetAddress(to)};
                msg.addRecipients(Message.RecipientType.TO, address);
                msg.setSubject(subject);
                Date date = null;
                msg.setSentDate(new Date());
                msg.setText(message);
                  System.out.println("3");
                Transport transport=mailSession.getTransport("smtp");
                transport.connect(host, user, password);
                  System.out.println("4");
                transport.sendMessage(msg, msg.getAllRecipients());
                  System.out.println("5");
                transport.close();
                System.out.println("Succesful");
                
                //Compose the message  
            } catch (MessagingException ex) {
                Logger.getLogger(SendMailBySite.class.getName()).log(Level.SEVERE, null, ex);
            }
  
 }  
}  


What I have tried:

when i run this program
error:-
Apr 17, 2017 5:41:51 PM SendMailBySite main
SEVERE: null
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e207sm17889011pfh.121 - gsmtp

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
	at SendMailBySite.main(SendMailBySite.java:54)

BUILD SUCCESSFUL (total time: 1 second)

Even i have closed all type of anti-virus as well i dis email setting as less security.
Posted
Updated 17-Apr-17 13:14pm
Comments
Anup KumarArya 17-Apr-17 8:28am    
please help me as soon as bcz on 19 april i have to submit my project in my University.
I have tried as much possible.
Richard MacCutchan 17-Apr-17 9:16am    
Look at the error message, it is telling you what you need to do.

1 solution

That's not a "null server" error. That's a SEVERE error, meaning that the code cannot continue. There error message is telling you exactly what to do. You should try reading it again and reading up on the STARTTLS command.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900