You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.3 KiB
96 lines
3.3 KiB
package de.memtext.hbt; |
|
|
|
import java.io.IOException; |
|
import java.util.Properties; |
|
|
|
import javax.mail.Folder; |
|
import javax.mail.Message; |
|
import javax.mail.MessagingException; |
|
import javax.mail.Session; |
|
import javax.mail.Store; |
|
import javax.mail.Transport; |
|
import javax.mail.internet.AddressException; |
|
import javax.mail.internet.InternetAddress; |
|
import javax.mail.internet.MimeMessage; |
|
|
|
public class Checker { |
|
|
|
public static void check(Session emailSession, String user, String password) |
|
throws MessagingException, IOException { |
|
|
|
// create the POP3 store object and connect with the pop server |
|
// pop3s wichtig -> vermutlich secure |
|
Store store = emailSession.getStore("pop3s"); |
|
store.connect(emailSession.getProperty("mail.pop3.host"), user, password); |
|
|
|
Folder emailFolder = store.getFolder("INBOX"); |
|
emailFolder.open(Folder.READ_ONLY); |
|
|
|
// messages werden geholt, können nur ausgelesen werden, wenn Folder noch open |
|
// ist! |
|
Message[] messages = emailFolder.getMessages(); |
|
|
|
System.out.println("messages.length---" + messages.length); |
|
|
|
for (int i = 0, n = messages.length; i < n; i++) { |
|
Message message = messages[i]; |
|
System.out.println("---------------------------------"); |
|
System.out.println("Email Number " + (i + 1)); |
|
System.out.println("Subject: " + message.getSubject()); |
|
System.out.println("From: " + message.getFrom()[0]); |
|
System.out.println("Text: " + message.getContent().toString()); |
|
|
|
} |
|
|
|
// close the store and folder objects |
|
emailFolder.close(false); |
|
store.close(); |
|
|
|
} |
|
|
|
private static void send(Session emailSession, String user, String recipient, String subject, String text, |
|
String password) throws AddressException, MessagingException { |
|
MimeMessage replyMessage = new MimeMessage(emailSession); |
|
// replyMessage = (MimeMessage) message.reply(false); |
|
replyMessage.setFrom(new InternetAddress(user)); |
|
replyMessage.setRecipients(Message.RecipientType.TO, recipient); |
|
replyMessage.setSubject(subject); |
|
replyMessage.setText(text); |
|
Transport t = emailSession.getTransport("smtp"); |
|
t.connect(user, password); |
|
t.sendMessage(replyMessage, replyMessage.getAllRecipients()); |
|
System.out.println("Message sent..."); |
|
} |
|
|
|
private static Session initSession(String pophost, String smtphost) { |
|
Properties properties = new Properties(); |
|
properties.put("mail.pop3.host", pophost); |
|
properties.put("mail.pop3.port", "995"); |
|
properties.put("mail.pop3.starttls.enable", "true"); |
|
properties.put("mail.smtp.auth", "true"); |
|
properties.put("mail.smtp.starttls.enable", "true"); |
|
properties.put("mail.smtp.host", smtphost); |
|
properties.put("mail.smtp.port", "25"); |
|
return Session.getDefaultInstance(properties); |
|
} |
|
|
|
public static void main(String[] args) { |
|
Session session = initSession("pop3.strato.de", "smtp.strato.de"); |
|
String username = "heartbeat@mbisping.de"; |
|
String password = "$Anfang1200"; |
|
|
|
try { |
|
check(session, username, password); |
|
send(session, username, "danielq@memtext.de", "From Düsseldorf with fun...", "This is the memtext's first digitial 'heartbeat' :-)\n send via java mail - \nThanks and see you around :-)", |
|
password); |
|
} catch (MessagingException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} catch (IOException e) { |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} |
|
|
|
} |
|
|
|
} |