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

发布于 2024-07-10 12:30:14 字数 1155 浏览 6 评论 0原文

我正在尝试使用 比尔蜥蜴代码以使用 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.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;

有人知道这个错误吗?

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.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)

Bill's code contains the next line, which seems related to the error:

   props.put("mail.smtp.starttls.enable","true");

However, it does not help.

These are my import statements:

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;

Does anyone know about this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

弄潮 2024-07-17 12:30:14

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

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

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

现在 Bill the Lizard 的代码可以工作了。

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.

这个俗人 2024-07-17 12:30:14

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

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

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"); 
才能让你更想念 2024-07-17 12:30:14

这是java邮件API的版本。
我遇到了这个问题,我刚刚将 java mail API 更新到 1.4.3
这对我来说可以!

谢谢!

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!

我很坚强 2024-07-17 12:30:14

我认为这与使用 SMTPS 而不是 SMTP 进行邮件传输有关。 这是一个不同的版本,以有关访问 Gmail 的 JavaMail 常见问题解答为蓝本。 请注意,为了清楚起见,我省略了所有更精细级别的异常处理。

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

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();
}
吃兔兔 2024-07-17 12:30:14

这让我发疯,所以只想添加对我有用的东西。 我必须更新我的 JavaMail 版本(1.4.5)才能正常工作 - 不确定之前使用的是哪个版本。

当我更新到新版本的 JavaMail 后,以下代码对我有用(可以取消注释调试行以获取其他调试信息 - 端口为 587,主机为 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();
    }
}

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();
    }
}
尴尬癌患者 2024-07-17 12:30:14

试试这个:

开启:允许您的 Gmail 帐户使用不太安全的应用

https://myaccount.google.com/lesssecureapps< /a>

try this:

turn on: Allow less secure apps for your gmail account

https://myaccount.google.com/lesssecureapps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文