ошибка выполнения. Проблема с SMTP-сервером - PullRequest
0 голосов
/ 28 апреля 2010

Привет, я запускаю следующую программу, получающую ошибку во время выполнения. Я вставил ниже.

import java.util.Properties;

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

public class MailWithPasswordAuthentication {
 public static void main(String[] args) throws MessagingException {
  new MailWithPasswordAuthentication().run();
 }

 private void run() throws MessagingException {
  Message message = new MimeMessage(getSession());

  message.addRecipient(RecipientType.TO, new InternetAddress("abc@yahoo.co.in"));
  message.addFrom(new InternetAddress[] { new InternetAddress("xyz@gmail.com") });

  message.setSubject("the subject");
  message.setContent("the body", "text/plain");

  Transport.send(message);
 }

 private Session getSession() {
  Authenticator authenticator = new Authenticator();

  Properties properties = new Properties();
  properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
  properties.setProperty("mail.smtp.auth", "true");

  properties.setProperty("mail.smtp.host", "smtp.gmail.com");
  properties.setProperty("mail.smtp.port", "25");

  return Session.getInstance(properties, authenticator);
 }

 private class Authenticator extends javax.mail.Authenticator {
  private PasswordAuthentication authentication;

  public Authenticator() {
   String username = "xyz@gmail.com";
   String password = "xyz";
   authentication = new PasswordAuthentication(username, password);
  }

  protected PasswordAuthentication getPasswordAuthentication() {
   return authentication;
  }
 }
}

Ошибка выполнения

Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. u8sm278510wbc.23

 at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
 at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
 at javax.mail.Transport.send0(Transport.java:191)
 at javax.mail.Transport.send(Transport.java:120)
 at MailWithPasswordAuthentication.run(MailWithPasswordAuthentication.java:26)
 at MailWithPasswordAuthentication.main(MailWithPasswordAuthentication.java:14)

Ответы [ 3 ]

2 голосов
/ 24 мая 2011

попробуйте добавить properties.setProperty("mail.smtp.starttls.enable", "true");

0 голосов
/ 28 апреля 2010

Использует ли Google порт 25 для SMTP?

Согласно этому документу от Google вам необходимо использовать порт 587 для TLS / STARTTLS или 465 для SSL.

0 голосов
/ 28 апреля 2010

Похоже, вы запрашиваете безопасное соединение и не выполняете его. Возможно, эта ссылка поможет?

...