Есть класс, в котором я подключаюсь к почте и нахожу письмо с определенной темой.
Дописала код, чтобы можно было ответить на письмо, но при прогоне выдает ошибку о невозможности подключиться. Код ниже
public class MailForReply {
public Message getEmail(String emailID, String password, String subjectToBeSearched) throws Exception {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
properties.put("mail.imaps.port", "993");
properties.put("mail.imaps.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("imaps");
store.connect("imap.gmail.com", emailID, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Message[] messages = null;
boolean mailFound = false;
Message email = null;
for (int i = 0; i < 30; i++) {
messages = folder.search(new SubjectTerm(subjectToBeSearched), folder.getMessages());
if (messages.length == 0) {
Thread.sleep(10000);
}
}
for (Message mail : messages) {
if (!mail.isSet(Flags.Flag.SEEN)) {
email = mail;
mailFound = true;
System.out.println("Mail is found");
}
}
String replyTo = InternetAddress.toString(email.getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}
String to = InternetAddress.toString(email.getRecipients(Message.RecipientType.TO));
if (to != null) {
System.out.println("To: " + to);
}
Message replyMessage = new MimeMessage(emailSession);
replyMessage = (MimeMessage) email.reply(false);
replyMessage.setFrom(new InternetAddress(to));
replyMessage.setText("Thanks");
replyMessage.setReplyTo(email.getReplyTo());
try {
Transport t = emailSession.getTransport("smtp");
t.connect("test@test.com", "1111111");
t.sendMessage(replyMessage,
replyMessage.getAllRecipients());
} finally {
store.close();
}
System.out.println("message replied successfully ....");
if (!mailFound) {
throw new Exception("Could not found Email");
}
return email;
}
}