Guten Tag/Abend!
Ich habe ein Problem mit der JavaMail API das Problem ist das ich bei start des Programms immer eine Exception bekomme!
Der Zweck des Programms besteht darin das man mit der login Methode eine Instance der Klasse erstellt und verschiedene Sachen mit den Emails machen kann.
Exception:
Code:
Was den Code angeht ist er nicht der beste.
Ich habe ein Problem mit der JavaMail API das Problem ist das ich bei start des Programms immer eine Exception bekomme!
Der Zweck des Programms besteht darin das man mit der login Methode eine Instance der Klasse erstellt und verschiedene Sachen mit den Emails machen kann.
Exception:
javax.mail.MessagingException: 220 smtp.gmail.com ESMTP k8sm4882723edn.52 - gsmtp;
nested exception is:
com.sun.mail.iap.ConnectionException: 220 smtp.gmail.com ESMTP k8sm4882723edn.52 - gsmtp
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:738)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at ********.Mail.<init>(Mail.java:80)
at ********.Mail.login(Mail.java:29)
at Main.main(Main.java:7)
Caused by: com.sun.mail.iap.ConnectionException: 220 smtp.gmail.com ESMTP k8sm4882723edn.52 - gsmtp
at com.sun.mail.imap.protocol.IMAPProtocol.processGreeting(IMAPProtocol.java:341)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:141)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
... 5 more
Code:
Java:
package ******;
import java.util.*;
import javax.mail.*;
import javax.mail.Flags.Flag;
import javax.mail.internet.*;
import com.sun.mail.imap.IMAPStore;
import *******.providers.Providers;
public class Mail {
private String email;
private Session session;
private IMAPStore imapStore;
private String username;
private int imap_port;
private String imap_host;
private int smtp_port;
private String smtp_host;
private int pop3_port;
private String pop3_host;
public static Mail login(String email, String password, String username, Providers provider) {
System.out.println("Anbieter: " + provider.getName());
provider.printInfos();
return new Mail(email, password, username, provider.getIMAPHost(), provider.getIMAPPort(), provider.getSMTPHost(), provider.getSMTPPort(), provider.getPOP3Host(), provider.getPOP3Port());
}
private Authenticator getAuthenticator(String email, String password) {
PasswordAuthentication passAuth = new PasswordAuthentication(email, password);
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return passAuth;
}
};
return auth;
}
private Properties getProps() {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", this.smtp_host);
props.put("mail.smtp.socketFactory.port", String.valueOf(this.smtp_port));
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", String.valueOf(this.smtp_port));
props.put("mail.store.protocol", "imaps");
props.put("mail.imaps.port", String.valueOf(this.imap_port));
props.put("mail.imaps.starttls.enable", "true");
props.put("mail.pop3.starttls.enable", "true");
props.put("mail.pop3.host", this.pop3_host);
props.put("mail.pop3.port", String.valueOf(this.pop3_port));
return props;
}
private Mail(String fromEmail, String password, String username, String imap_host, int imap_port, String smtp_host, int smtp_port, String pop3_host, int pop3_port) {
try {
this.imap_host = imap_host;
this.imap_port = imap_port;
this.smtp_host = smtp_host;
this.smtp_port = smtp_port;
this.pop3_host = pop3_host;
this.pop3_port = pop3_port;
System.out.println("Einloggen...");
session = Session.getDefaultInstance(getProps(), getAuthenticator(fromEmail, password));
Store store = session.getStore("imaps");
store.connect(this.imap_host, username, password);
imapStore = (IMAPStore) store;
System.out.println("Eingeloggt!");
System.out.println("E-Mail: " + fromEmail);
System.out.println("Benutzername: " + username);
System.out.println("Passwort: " + password);
} catch(Exception e) {
e.printStackTrace();
}
}
public void send(String email, String subject, String text, String description, String charset) {
try {
MimeMessage message = new MimeMessage(session);
System.out.println("Versende...");
message.setFrom(new InternetAddress(this.email, username));
message.setReplyTo(InternetAddress.parse(email, false));
message.setSubject(subject);
message.setText(text, charset);
message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
message.setSentDate(new Date());
message.setDescription(description, charset);
Transport.send(message);
System.out.println("E-Mail versendet!");
} catch(Exception e) {
e.printStackTrace();
}
}
public void check() {
try {
Folder folder = imapStore.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
System.out.println("Nachrichten: " + folder.getMessageCount());
System.out.println("Neue Nachrichten: " + folder.getNewMessageCount());
System.out.println("Ungelesene Nachrichten: " + folder.getUnreadMessageCount());
System.out.println("Gelöschte Nachrichten: " + folder.getDeletedMessageCount());
} catch(Exception e) {
e.printStackTrace();
}
}
public void checkEmail(int i) {
try {
Folder folder = imapStore.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message = folder.getMessage(i);
if(!message.isSet(Flag.SEEN)) {
System.out.println("Datum/Uhrzeit: " + message.getReceivedDate());
System.out.println("Von: " + Arrays.toString(message.getFrom()));
System.out.println("Betreff: " + message.getSubject());
System.out.println("Inhalt: " + message.getContent());
System.out.println("Beschreibung: " + message.getDescription());
}
} catch(Exception e) {
e.printStackTrace();
}
}
public void checkEmails() {
try {
Folder folder = imapStore.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
for(int i = 1; i < folder.getMessageCount(); i++) {
System.out.println("====================================");
checkEmail(i);
System.out.println("====================================");
}
} catch(Exception e) {
e.printStackTrace();
}
}
public String[] getEmails() {
try {
Folder folder = imapStore.getFolder("INBOX");
String[] str = new String[folder.getMessageCount()];
folder.open(Folder.READ_ONLY);
for(int i = 1; i < folder.getMessageCount(); i++) {
str[i] = folder.getMessage(i).getSubject();
}
return str;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
public void setEmailFlag(int i, Flag flag, boolean bool) {
try {
Folder folder = imapStore.getFolder("INBOX");
folder.open(Folder.HOLDS_FOLDERS);
folder.getMessage(i).setFlag(flag, bool);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Was den Code angeht ist er nicht der beste.