Я не могу отправить электронное письмо с кодом Java через Gmail.Не удается отправить электронное письмо через Gmail с помощью Java
КОД:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class b {
public static void sendMessage(String to, String from, String subject, String text){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(text);
Transport transport = session.getTransport("smtp");
String mfrom = "fromemail";
transport.connect("smtp.gmail.com", mfrom, "fromemailpassword");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Я получаю ошибку:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. b3sm24500496wiw.22 - gsmtp
Я пытаюсь отправить сообщение электронной почты с учетной записью Gmail в сведенью Gmail без успеха. Мне нужно сделать это для моего электронного письма электронной почты для рассылки информационных бюллетеней и т. Д.
EDIT:
Я изменил раздел кода следующим образом:
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true"); //make it true
properties.put("mail.smtp.starttls.enable", "true"); //make it true
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
//Authentication is needed use your gmail user and password"smtp.gmail.com"
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "password");
}
};
Session session = Session.getInstance(properties, auth);
И теперь я получаю исключение: javax.mail.AuthenticationFailedException
вы поищите в Google в первую очередь? многие нетребовательные примеры этого. см. [FAQ] по необходимости поискать первый – tgkprog
возможный дубликат свойств JavaMail smtp (для STARTTLS)] (http://stackoverflow.com/questions/5592112/javamail-smtp-properties-for-starttls) – tgkprog