Translate

9 Aralık 2014 Salı

Unable to send email using JavaMail API:getting error Must issue a STARTTLS command


I am sharing this post with my research and observation,because I have faced this situation


Somebody solve this another way , by allowing admins to enter encryption type(allowed values are '', 'ssl' and 'tls').

BUT,There was no way to set encryption type in outbound SMTP settings.I did use starttls but it didn't work I hope it is because some access from localhost ,because of antivirus programs.
But most important think is jar files and classpath (buildpath) dont't ever duplicate mail-1.4.5.jar and smtp.jar and others

package com.test.cal;

import java.util.Properties;
import java.util.Scanner;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendEmail {

 public static void main(String[] args) {

  // Create a SendEmail object and call start
  // method to send a mail in Java.
  SendEmail sendEmail = new SendEmail();
  sendEmail.start();

 }

 private void start() {

  // For establishment of email client with
  // Google's gmail use below properties.
  // For TLS Connection use below properties
  // Create a Properties object
  Properties props = new Properties();

 
  // For SSL Connection use below properties

   props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.socketFactory.port", "465");
  props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.port", "465");

  // Create Scanner object to take necessary
  // values from the user.
  Scanner scanner = new Scanner(System.in);

  System.out.println("Please provide your Username for Authentication ...");
  final String Username = scanner.next();

  System.out.println("Please provide your Password for Authentication ...");
  final String Password = scanner.next();

  System.out.println("Please provide Email Address from which you want to send Email ...");
  final String fromEmailAddress = scanner.next();

  System.out.println("Please provide Email Address to which you want to send Email ...");
  final String toEmailAddress = scanner.next();

  System.out.println("Please provide Subject for your Email ... ");
  final String subject = scanner.next();

  System.out.println("Please provide Text Message for your Email ... ");
  final String textMessage = scanner.next();

  // Create a Session object based on the properties and
  // Authenticator object
  Session session = Session.getDefaultInstance(props,
    new LoginAuthenticator(Username,Password));

  try {

   // Create a Message object using the session created above
   Message message = new MimeMessage(session);

   // setting email address to Message from where message is being sent
   message.setFrom(new InternetAddress(fromEmailAddress));

   // setting the email address to which user wants to send message
   message.setRecipients(Message.RecipientType.TO,
     InternetAddress.parse(toEmailAddress));

   // setting the subject for the email
   message.setSubject(subject);

   // setting the text message which user wants to send to recipients
   message.setText(textMessage);

   // Using the Transport class send() method to send message
   Transport.send(message);

   System.out.println("\nYour Message delivered successfully ....");

  } catch (MessagingException e) {

   throw new RuntimeException(e);

  }
 }

 // Creating a class for Username and Password authentication
 // provided by the user.
 class LoginAuthenticator extends Authenticator {
  PasswordAuthentication authentication = null;

  public LoginAuthenticator(String username, String password) {
   authentication = new PasswordAuthentication(username,password);
  }

  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
   return authentication;
  }
 }

}

- Source at: http://www.hubberspot.com/2012/12/how-to-send-email-in-java-using-java.html#sthash.nbRJP79c.dpuf

Hiç yorum yok:

Yorum Gönder

Bu Blogda Ara