Must issue a STARTTLS command first. Sending email with Java and Google Apps

前端 未结 6 2009
傲寒
傲寒 2020-11-30 05:56

I am trying to use Bill the Lizard\'s code to send an email using Google Apps. I am getting this error:

Exception in thread \"main\" javax.mail.SendFailedEx         


        
相关标签:
6条回答
  • 2020-11-30 06:22

    I think it's got to do with using SMTPS instead of SMTP for mail transport. Here's a different version, modeled after the JavaMail FAQ on accessing Gmail. Note that I have left out all of the finer level exception handling for clarity.

    private static void send(
            final String username,
            final String password,
            final String recipients,
            final String subject,
            final String body)
            throws Exception
    {
        final Session session = Session.getInstance(System.getProperties(), null);
        final Message msg = new MimeMessage(session);
        final String senderEmail = username.contains("@") ? username : (username + "@gmail.com");
        msg.setFrom(new InternetAddress(senderEmail));
    
        final Address[] recipientAddresses = InternetAddress.parse(recipients);
        msg.setRecipients(Message.RecipientType.TO, recipientAddresses);
    
        msg.setSentDate(new Date());
        msg.setSubject(subject);
        msg.setText(body);
    
        final Transport transport = session.getTransport("smtps");
        transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password);
        transport.sendMessage(msg, recipientAddresses);
        transport.close();
    }
    
    0 讨论(0)
  • 2020-11-30 06:25

    It is the version of java mail API. I was facing this issue and I just updated the java mail API to 1.4.3 It works fine for me!

    Thanks!

    0 讨论(0)
  • 2020-11-30 06:25

    try this:

    turn on: Allow less secure apps for your gmail account

    https://myaccount.google.com/lesssecureapps

    0 讨论(0)
  • 2020-11-30 06:30

    Set the following tags. It will work.

    props.put("mail.smtp.user","username"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "25"); 
    props.put("mail.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.starttls.enable","true"); 
    props.put("mail.smtp.EnableSSL.enable","true");
    
    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");   
    props.setProperty("mail.smtp.socketFactory.fallback", "false");   
    props.setProperty("mail.smtp.port", "465");   
    props.setProperty("mail.smtp.socketFactory.port", "465"); 
    
    0 讨论(0)
  • 2020-11-30 06:40

    I found the problem. Previously i was using j2ee.jar to import javax.mail.

    I removed j2ee.jar from the classpath and downloaded JavaMail 1.4.1 and put into my classpath two jars, smtp.jar and mailapi.jar. I use now smtps instead smtp

    Transport transport = session.getTransport("smtps");            
    

    Now Bill the Lizard's code works.

    0 讨论(0)
  • 2020-11-30 06:40

    This was driving me crazy and so just wanted to add what worked for me. I had to update my version of JavaMail (1.4.5) for this to work - not sure what version was being used before.

    Once I updated to new version of JavaMail, the following code worked for me (can uncomment the debug lines to get additional debugging info - port was 587 and host was smtp.gmail.com):

    public void sendMailWithAuth(String host, String user, String password, 
        String port, List<String> toList, String htmlBody, 
            String subject) throws Exception {
    
        Properties props = System.getProperties();
    
        props.put("mail.smtp.user",user); 
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.host", host); 
        props.put("mail.smtp.port", port); 
        //props.put("mail.debug", "true"); 
        props.put("mail.smtp.auth", "true"); 
        props.put("mail.smtp.starttls.enable","true"); 
        props.put("mail.smtp.EnableSSL.enable","true");
    
        Session session = Session.getInstance(props, null);
        //session.setDebug(true);
    
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(user));
    
        // To get the array of addresses
        for (String to: toList) {
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        }
    
        message.setSubject(subject);
        message.setContent(htmlBody, "text/html");
    
        Transport transport = session.getTransport("smtp");
        try {
            transport.connect(host, user, password);
            transport.sendMessage(message, message.getAllRecipients());
        } finally {
            transport.close();
        }
    }
    
    0 讨论(0)
提交回复
热议问题