Я пытаюсь создать базовое c приложение, которое отправляет электронное письмо кому-либо одним нажатием кнопки.
Я использую активация.jar и javax.mail.jar, оба из которых настроены для использования в eclipse в свойствах> Java Путь сборки> Добавить внешние банки.
I нет ошибок в моем коде, все выглядит нормально. Когда я пытаюсь запустить программу, я получаю сообщение об ошибке, указанное в заголовке.
Вот мой код:
package me.hunter;
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;
public class InstaEmail {
public static void main(String args[]) throws Exception {
email("xx@xx.com");
}
public static void email(String recepient) throws Exception {
System.out.println("Email sending...");
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.auth", "587");
String email = "xx@xx.com";
String password = "xxxxxxx";
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
});
Message message = prepareMessage(session, email, recepient);
try {
Transport.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Message sent!");
}
private static Message prepareMessage(Session session, String email, String recepient) {
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
message.setSubject("Need Help Promoting X!");
message.setText("Hey there, \n I want stickers!");
} catch(Exception ex) {
System.out.println(ex);
}
return null;
}
}
Я нашел множество решений моей проблемы, но ни один из них не принес успеха в моем случае. Заранее спасибо!