hi i am unable to run this java mail code can any body tell me whats wrong in tis code or may be whats the error!!
public class Class1 { final String senderEmailID = "from@gmail.com"; final String senderPassword = "password"; final String emailSMTPserver = "smtp.gmail.com"; final String emailServerPort = "465"; String receiverEmailID = null; String emailSubject = null; String emailBody = null; public Class1(String receiverEmailID, String emailSubject, String emailBody) { this.receiverEmailID=receiverEmailID; this.emailSubject=emailSubject; this.emailBody=emailBody; Properties props = new Properties(); props.put("mail.smtp.user",senderEmailID); props.put("mail.smtp.host", emailSMTPserver); props.put("mail.smtp.port", emailServerPort); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); SecurityManager security; security = System.getSecurityManager(); System.out.println("security"); try { Authenticator auth; auth = new SMTPAuthenticator(); Session session; session = Session.getInstance(props, auth); MimeMessage msg; msg = new MimeMessage(session); msg.setText(emailBody); msg.setSubject(emailSubject); msg.setFrom(new InternetAddress(senderEmailID)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiverEmailID)); Transport.send(msg); } catch (Exception mex) { mex.printStackTrace(); }} private class SMTPAuthenticator extends javax.mail.Authenticator { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(senderEmailID, senderPassword); } } public static void main(String[] args) { System.out.println("start"); Class1 class1; class1 = new Class1("to@gmail.com","Test Mail from me","Here goes the body"); System.out.println("end"); } }
i get an error as this
any help appreciatedjavax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at Class1.<init>(Class1.java:69) at Class1.main(Class1.java:90) end Process exited with exit code 0.