/**
* Connect to mail client
*
* @param host
* @param port
* @param userName
* @param password
* @return
*/
public boolean getConnection(String host, String port, String userName, String password) {
Properties properties = new Properties();
// ---------- Server Setting---------------
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
properties.put("mail.smtp.ssl.enable", "true");
// ---------- SSL setting------------------
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port));
Session session = Session.getDefaultInstance(properties);
if (!isconnected) {
try {
// Get the POP3 store provider and connect to the store.
store = session.getStore("pop3");
store.connect(userName, password);
isconnected = true;
// opens the inbox folder
System.out.println("Getting INBOX..");
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
System.out.println("Connected to mail client: " + isconnected);
System.out.println("Connected to mail via " + host);
} catch (Exception e) {
e.printStackTrace();
}
}
return isconnected;
}
public String getSubject() {
String subject = "";
try {
if (!isconnected) {
getConnection(host, port, userName, password);
}
// fetches new messages from server
messages = inbox.getMessages();
System.out.println("You have " + messages.length + " mails in your INBOX");
int i = messages.length - 1;
Message message = messages[i];
subject = message.getSubject();
System.out.println("Mail Subject: " + subject);
} catch (Exception e) {
e.printStackTrace();
}
return subject;
}
public String getBodyContent() {
String messageContent = "";
try {
if (!isconnected) {
getConnection(host, port, userName, password);
}
messages = inbox.getMessages();
int i = messages.length - 1;
Message message = messages[i];
String contentType = message.getContentType();
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
messageContent = getText(message);
} else {
// this part for the message content
messageContent = part.getContent().toString();
}
}
} else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Mail Body Message: " + messageContent);
return messageContent;
}
public String getAttachmentFileName() {
String fileName = "";
try {
if (!isconnected) {
getConnection(host, port, userName, password);
}
messages = inbox.getMessages();
int i = messages.length - 1;
Message message = messages[i];
String contentType = message.getContentType();
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
fileName = part.getFileName();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Attachment File Name: " + fileName);
return fileName;
}
public static String getText(Part p) {
try {
if (p.isMimeType("text/*")) {
String s = (String) p.getContent();
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void deleteEmails() throws Exception {
try {
if (!isconnected) {
getConnection(host, port, userName, password);
}
messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
message.setFlag(Flags.Flag.DELETED, true);
}
inbox.close(true);
store.close();
System.out.println("deleted mail");
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeMailConnections() throws MessagingException {
if (null != inbox && null != store) {
try {
inbox.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment