java javamail 错误:必须首先发出 starttls 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2964296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:43:11  来源:igfitidea点击:

javamail error: must issue starttls command first

javajavamail

提问by simplyblue

I'm trying to send a mail using javamail api using the below code: when I compile the class file I'm getting the below error which says 'must issue starttls command first' I have mentioned the error below. And also getProvider()function error I think so... I don' t know what the errors mean.

我正在尝试使用以下代码使用 javamail api 发送邮件:当我编译类文件时,我收到以下错误,提示“必须首先发出 starttls 命令”我在下面提到了错误。还有getProvider()函数错误,我认为是这样...我不知道这些错误是什么意思。

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import javax.mail.Authenticator;
import java.net.*;
import java.util.Properties;
public class mailexample 
    {
  public static void main (String args[]) throws Exception {

    String from = args[0];
    String to = args[1];
try
{
Properties props=new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
    {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
        {
        return new javax.mail.PasswordAuthentication("[email protected]", "pass");
    }
};
Session sess=Session.getDefaultInstance(props,authenticator);
sess.setDebug (true);
Transport transport =sess.getTransport ("smtp");
Message msg=new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Hello JavaMail");
msg.setText("Welcome to JavaMail");
transport.connect();
transport.send(msg);

}
catch(Exception e)
{
System.out.println("err"+e);
}
}
}

error:

错误:

C:\Users\bobby\Desktop>java mailexample [email protected] abc@gmail.
com

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

DEBUG SMTP SENT: MAIL FROM:
DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb
.16

DEBUG SMTP SENT: QUIT
errjavax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f
irst. l29sm12930755rvb.16

回答by YOU

Probably, you need to put props.put("mail.smtp.starttls.enable","true");before you authenticate.

可能,您需要props.put("mail.smtp.starttls.enable","true");在进行身份验证之前放置。

回答by Suhrud B

This code seems to work fine using the Gmail SMTP server to send emails. Note - This does not have an attachment.

使用 Gmail SMTP 服务器发送电子邮件时,此代码似乎可以正常工作。注意 - 这没有附件。

(Source : Modified from the example on https://forums.oracle.com/forums/thread.jspa?threadID=1587188)

(来源:修改自https://forums.oracle.com/forums/thread.jspa?threadID=1587188上的示例)

package org.ssb.mail;

import java.util.Date;
import java.util.Properties;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailClient {

/**
 * Entry method
 * 
 * @param args
 *            String[]
 */
public static void main(String[] args) {

    MailClient client = new MailClient();

    try {
        client.sendMail();
    } catch (AddressException ae) {
        ae.printStackTrace();
    } catch (MessagingException me) {
        me.printStackTrace();
    }

}

/**
 * Sends an email
 * 
 * @param none
 */
private void sendMail() throws AddressException, MessagingException {

    // Get a Properties object
    Properties props = System.getProperties();

    // ******************** FOR PROXY ******************

    // props.setProperty("proxySet","true");
    // props.setProperty("socksProxyHost","9.10.11.12");
    // props.setProperty("socksProxyPort","80");
    // props.setProperty("socksProxyVersion","5");

    props.setProperty("mail.smtp.host", "smtp.gmail.com");

    // ******************** FOR SSL ******************
    //final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    //props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    //props.setProperty("mail.smtp.socketFactory.fallback", "false");
    //props.setProperty("mail.smtp.port", "465");
    //props.setProperty("mail.smtp.socketFactory.port", "465");

    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.debug", "true");
    props.put("mail.store.protocol", "pop3");
    props.put("mail.transport.protocol", "smtp");
    final String username = "sender-username";
    final String password = "sender-password";
    Session session = Session.getDefaultInstance(props,
            new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    // -- Create a new message --
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("[email protected]"));
    msg.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("[email protected]", false));
    msg.setSubject("Hello");
    msg.setSentDate(new Date());

    // **************** Without Attachments ******************
    msg.setText("How are you");


    Transport.send(msg);
    System.out.println("Message sent.");

}

}

回答by user3029106

I had the same problem as you described. In my case, replacing

我遇到了与您描述的相同的问题。就我而言,更换

Session session = Session.getDefaultInstance(props);

Session session = Session.getDefaultInstance(props);

with

Session session = Session.getInstance(props);

Session session = Session.getInstance(props);

did the trick. I hope it will be helpful.

成功了。我希望它会有所帮助。

回答by Dinum Dissanayaka

This work for me. 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"); 

回答by Ajay Takur

mail.jar upto 1.4 v dont need props.put("mail.smtp.starttls.enable","true");later versions it is required to setup.

mail.jar up props.put("mail.smtp.starttls.enable","true");to 1.4 v 不需要更高版本,它需要设置。