получено исключение: javax.mail.MessagingException: Невозможно отправить команду на хост SMTP; - PullRequest
0 голосов
/ 05 марта 2020
public class Sendmail {
  static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;
static String userEmail="deepakkumar21158@gmail.com";
static String userPassword="9717255435";   
public static void generateAndSendEmail() throws AddressException, MessagingException{
    System.out.println("\n 1st ===> setup Mail Server Properties.."+userEmail+" ===== "+userPassword);
    mailServerProperties = System.getProperties();
    mailServerProperties.put("mail.smtp.starttls.enable", "true");
    mailServerProperties.put("mail.smtp.port", "587");
    mailServerProperties.put("mail.smtp.auth", "true");
            mailServerProperties.put("mail.smtp.ssl.trust", "smtp.gmail.com");

    System.out.println("Mail Server Properties have been setup successfully..");

    // Step2
    //System.out.println("\n\n 2nd ===> get Mail Session.."+row.getCell((int) mp.get("Name")));
    getMailSession = Session.getDefaultInstance(mailServerProperties, null);
    generateMailMessage = new MimeMessage(getMailSession);

    //String recpEmail=row.getCell(mp.get("Email_Address")).toString();
    generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("deepaksingh21168@gmail.com"));

    generateMailMessage.setSubject("CDAM");


    StringBuilder emailBody = new StringBuilder();

// emailBody.append («Привет» + row.getCell ((int) mp.get («Имя»)) + «всплывающее окно \ r \ n»); // emailBody.append («ваш продукт» + row.getCell ((int) mp.get («Product»))); // emailBody.append («истекает» + row.getCell ((int) mp.get ("Expiry_Date"))); emailBody.append ("Hogaya");

    generateMailMessage.setContent(emailBody.toString(), "text/html");
    System.out.println("Mail Session has been created successfully..");

    // Step3
    System.out.println("\n\n 3rd ===> Get Session and Send mail");
    Transport transport = getMailSession.getTransport("smtp");
            System.out.println("2");

    // Enter your correct gmail UserID and Password
    // if you have 2FA enabled then provide App Specific Password
    transport.connect("smtp.gmail.com",userEmail,userPassword);
            System.out.println("3");
    transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
    transport.close();
    System.out.println("hogaya");
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

      try {
          generateAndSendEmail();
      } catch (MessagingException ex) {
          System.err.println("Hello ====>>>> "+ex);
      }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...