Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I worked Steven's example a little bit and removed the parts of the code specific to Steven. My code won't read the body of an email if it has attachments. That is fine for my case but you may want to refine it further for yours.</p> <pre><code>package utils; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import javax.mail.Address; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.MimeBodyPart; public class IncomingMail { public static List&lt;Email&gt; downloadPop3(String host, String user, String pass, String downloadDir) throws Exception { List&lt;Email&gt; emails = new ArrayList&lt;Email&gt;(); // Create empty properties Properties props = new Properties(); // Get the session Session session = Session.getInstance(props, null); // Get the store Store store = session.getStore("pop3"); store.connect(host, user, pass); // Get folder Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); try { // Get directory listing Message messages[] = folder.getMessages(); for (int i = 0; i &lt; messages.length; i++) { Email email = new Email(); // from email.from = messages[i].getFrom()[0].toString(); // to list Address[] toArray = messages[i] .getRecipients(Message.RecipientType.TO); for (Address to : toArray) { email.to.add(to.toString()); } // cc list Address[] ccArray = null; try { ccArray = messages[i] .getRecipients(Message.RecipientType.CC); } catch (Exception e) { ccArray = null; } if (ccArray != null) { for (Address c : ccArray) { email.cc.add(c.toString()); } } // subject email.subject = messages[i].getSubject(); // received date if (messages[i].getReceivedDate() != null) { email.received = messages[i].getReceivedDate(); } else { email.received = new Date(); } // body and attachments email.body = ""; Object content = messages[i].getContent(); if (content instanceof java.lang.String) { email.body = (String) content; } else if (content instanceof Multipart) { Multipart mp = (Multipart) content; for (int j = 0; j &lt; mp.getCount(); j++) { Part part = mp.getBodyPart(j); String disposition = part.getDisposition(); if (disposition == null) { MimeBodyPart mbp = (MimeBodyPart) part; if (mbp.isMimeType("text/plain")) { // Plain email.body += (String) mbp.getContent(); } } else if ((disposition != null) &amp;&amp; (disposition.equals(Part.ATTACHMENT) || disposition .equals(Part.INLINE))) { // Check if plain MimeBodyPart mbp = (MimeBodyPart) part; if (mbp.isMimeType("text/plain")) { email.body += (String) mbp.getContent(); } else { EmailAttachment attachment = new EmailAttachment(); attachment.name = decodeName(part.getFileName()); File savedir = new File(downloadDir); savedir.mkdirs(); // File savefile = File.createTempFile( "emailattach", ".atch", savedir); File savefile = new File(downloadDir,attachment.name); attachment.path = savefile.getAbsolutePath(); attachment.size = saveFile(savefile, part); email.attachments.add(attachment); } } } // end of multipart for loop } // end messages for loop emails.add(email); // Finally delete the message from the server. messages[i].setFlag(Flags.Flag.DELETED, true); } // Close connection folder.close(true); // true tells the mail server to expunge deleted messages store.close(); } catch (Exception e) { folder.close(true); // true tells the mail server to expunge deleted store.close(); throw e; } return emails; } private static String decodeName(String name) throws Exception { if (name == null || name.length() == 0) { return "unknown"; } String ret = java.net.URLDecoder.decode(name, "UTF-8"); // also check for a few other things in the string: ret = ret.replaceAll("=\\?utf-8\\?q\\?", ""); ret = ret.replaceAll("\\?=", ""); ret = ret.replaceAll("=20", " "); return ret; } private static int saveFile(File saveFile, Part part) throws Exception { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(saveFile)); byte[] buff = new byte[2048]; InputStream is = part.getInputStream(); int ret = 0, count = 0; while ((ret = is.read(buff)) &gt; 0) { bos.write(buff, 0, ret); count += ret; } bos.close(); is.close(); return count; } } </code></pre> <p>You also need these two helper classes</p> <pre><code>package utils; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Email { public Date received; public String from; public List&lt;String&gt; to = new ArrayList&lt;String&gt;(); public List&lt;String&gt; cc = new ArrayList&lt;String&gt;(); public String subject; public String body; public List&lt;EmailAttachment&gt; attachments = new ArrayList&lt;EmailAttachment&gt;(); } </code></pre> <p>and</p> <pre><code>package utils; public class EmailAttachment { public String name; public String path; public int size; } </code></pre> <p>I used this to test the above classes</p> <pre><code>package utils; import java.util.List; public class Test { public static void main(String[] args) { String host = "some host"; String user = "some user"; String pass = "some pass"; String downloadDir = "/Temp"; try { List&lt;Email&gt; emails = IncomingMail.downloadPop3(host, user, pass, downloadDir); for ( Email email : emails ) { System.out.println(email.from); System.out.println(email.subject); System.out.println(email.body); List&lt;EmailAttachment&gt; attachments = email.attachments; for ( EmailAttachment attachment : attachments ) { System.out.println(attachment.path+" "+attachment.name); } } } catch (Exception e) { e.printStackTrace(); } } } </code></pre> <p>More info can be found at <a href="http://java.sun.com/developer/onlineTraining/JavaMail/contents.html" rel="noreferrer">http://java.sun.com/developer/onlineTraining/JavaMail/contents.html</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload