必须首先发出STARTTLS命令。使用Java和Google应用发送电子邮件

必须首先发出STARTTLS命令。使用Java和Google应用发送电子邮件,java,email,google-apps,Java,Email,Google Apps,我正在尝试使用谷歌应用程序发送电子邮件。我得到这个错误: Exception in thread "main" javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74 at javax.mail.Tra

我正在尝试使用谷歌应用程序发送电子邮件。我得到这个错误:

Exception in thread "main" javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. f3sm9277120nfh.74

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81)
    at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44)
比尔的代码包含下一行,它似乎与错误有关:

   props.put("mail.smtp.starttls.enable","true");
然而,这并没有帮助

以下是我的进口声明:

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

有人知道这个错误吗?

我认为这与使用SMTPS而不是SMTP进行邮件传输有关。这是一个不同的版本,模仿了。注意,为了清晰起见,我省略了所有更精细级别的异常处理

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();
}

我发现了问题。以前我使用j2ee.jar导入javax.mail

我从类路径中删除了j2ee.jar,下载了1.4.1,并将两个jar,smtp.jar和mailapi.jar放入我的类路径中。我现在使用smtps而不是smtp

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

现在,蜥蜴比尔的密码起作用了

它是java邮件API的版本。 我正面临这个问题,我刚刚将java邮件API更新为1.4.3 这对我来说很好


谢谢

设置以下标记。它会起作用的

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"); 

这让我发疯,所以我想补充一下对我有用的东西。我必须更新我的JavaMail版本(1.4.5)才能工作——不确定以前使用的是哪个版本

当我更新到新版本的JavaMail后,以下代码对我很有用(可以取消对调试行的注释以获取其他调试信息-端口为
587
,主机为
smtp.gmail.com
):

public void sendMailWithAuth(字符串主机、字符串用户、字符串密码、,
字符串端口、列表列表、字符串htmlBody、,
字符串主题)引发异常{
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=新MimeMessage(会话);
message.setFrom(新Internet地址(用户));
//获取地址数组的步骤
for(字符串到:toList){
message.addRecipient(message.RecipientType.TO,新Internet地址(TO));
}
message.setSubject(主题);
message.setContent(htmlBody,“text/html”);
传输=session.getTransport(“smtp”);
试一试{
传输连接(主机、用户、密码);
transport.sendMessage(message,message.getAllRecipients());
}最后{
transport.close();
}
}
试试这个:

启用:允许您的gmail帐户使用不太安全的应用程序


我在运行代码时遇到下一个异常:线程“main”javax.mail.NoSuchProviderException中的异常:没有像JavaMail资源文件这样的SMTPSSEMS提供程序丢失或损坏(请参阅)。mail.jar/META-INF中有文件的默认副本。这似乎是合理的,但为什么要处理端口25呢?它通常被ISP屏蔽,几行之后又被重置为465…我在使用1.4.2时遇到了同样的问题,更新到1.4.3并不是一个解决方案。不确定是否存在版本问题。
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();
    }
}