JavaMail API Fundamentals John Zukowski - jaz@zukowski.net JZ Ventures, Inc.
Agenda <ul><li>Getting Started </li></ul><ul><li>Core Classes </li></ul><ul><li>Getting Mail </li></ul><ul><li>Sending Mai...
Getting Started <ul><li>Without (Pre?) JavaMail </li></ul><ul><li>With JavaMail </li></ul><ul><ul><li>http://java.sun.com/...
Without JavaMail <ul><li>Read RFC 821 for SMTP </li></ul><ul><ul><li>http://www.cis.ohio-state.edu/htbin/rfc/rfc821.html <...
SmtpClient <ul><li>import sun.net.smtp.SmtpClient;  import java.io.PrintStream; </li></ul><ul><li>public class SmtpClientE...
Without JavaMail Take 3 <ul><li>Just use a URL </li></ul><ul><li>Open a mailto: URL </li></ul><ul><li>Write to opened URLC...
Mail Through URL <ul><li>import java.io.*; </li></ul><ul><li>import java.net.*; </li></ul><ul><li>public class MailTo { </...
With JavaMail API <ul><li>Latest Version 1.2 </li></ul><ul><ul><li>December 5, 2000 release </li></ul></ul><ul><ul><li>Sun...
JavaMail Setup <ul><li>Add JAR files </li></ul><ul><ul><li>to CLASSPATH, to jre/lib/ext </li></ul></ul><ul><ul><li>Applets...
JDK Requirements <ul><li>Works with JDK 1.1.6+ </li></ul><ul><li>Works with Java 2 platform, Standard Edition, versions 1....
Core Classes <ul><li>Session </li></ul><ul><li>Message / MimeMessage </li></ul><ul><li>InternetAddress </li></ul><ul><li>A...
Session <ul><li>Represents a mail session </li></ul><ul><li>Uses Properties to get things like mail host </li></ul><ul><ul...
Message / MimeMessage <ul><li>Represents a mail message </li></ul><ul><ul><li>Message abstract class </li></ul></ul><ul><u...
InternetAddress <ul><li>RFC822 Address </li></ul><ul><li>Create: </li></ul><ul><ul><li>new InternetAddress(&quot;jaz@zukow...
Authenticator <ul><li>Permit mechanism to prompt for username and password </li></ul><ul><ul><li>javax.mail.Authenticator ...
Transport <ul><li>Message transport mechanism </li></ul><ul><li>Get transport for session </li></ul><ul><ul><li>Transport ...
Sending Mail <ul><li>Need a working SMTP server </li></ul><ul><ul><li>Can be written in Java using JavaMail API, but irrel...
Hello World <ul><li>import java.util.Properties; </li></ul><ul><li>import javax.mail.*; </li></ul><ul><li>import javax.mai...
Hello World/2 <ul><li>// Get session </li></ul><ul><li>Session session = Session.getInstance(props, null); </li></ul><ul><...
Getting Mail <ul><li>POP3 provider doesn’t provide local data storage </li></ul><ul><li>There are mailbox store providers ...
Reading Hello World <ul><li>import java.io.*; </li></ul><ul><li>import java.util.Properties; </li></ul><ul><li>import java...
Reading Hello World/2 <ul><li>// Get the store </li></ul><ul><li>Store store = session.getStore(&quot;pop3&quot;); </li></...
Reading Hello World/3 <ul><li>System.out.println(i + &quot;: &quot; + message[i].getFrom()[0]  </li></ul><ul><li>+ &quot;	...
Authenticator Usage <ul><li>Put host in properties </li></ul><ul><ul><li>Properties props = new Properties(); </li></ul></...
Saving Messages <ul><li>To save copy locally: </li></ul><ul><ul><li>Get/create appropriate provider </li></ul></ul><ul><ul...
Replying <ul><li>Use Message.reply(boolean) </li></ul><ul><ul><li>Sets up message with proper headers </li></ul></ul><ul><...
Deleting Messages <ul><li>Set message flag to deleted: </li></ul><ul><ul><li>message.setFlag(Flags.Flag.DELETED, true); </...
Including Attachments <ul><li>Each attachment goes into a MimeBodyPart </li></ul><ul><li>DataHandler deals with reading in...
Sending Attachments <ul><li>// create mime message object and set the required parameters </li></ul><ul><li>MimeMessage me...
Sending Attachments/2 <ul><li>for( int i = 0; i < attachments.length; i++ ) { </li></ul><ul><li>messageBodyPart = new Mime...
Sending HTML Attachment <ul><li>Don’t use setText() </li></ul><ul><li>Use setDataHandler() </li></ul><ul><ul><li>String ht...
Including HTML Images Inline <ul><li>Specify Image source with cid: URL </li></ul><ul><ul><li><IMG SRC=cid:23abc@pc27> </l...
Getting Attachments from Client <ul><li>You want to create a web-based email system </li></ul><ul><li>Your user wants to i...
HTML Form <ul><li><FORM ENCTYPE=&quot;multipart/form-data&quot; method=post action=&quot;/myservlet&quot;>  </li></ul><ul>...
Getting Attachments <ul><li>For each part of Multipart, process part </li></ul><ul><li>Attachments can be inline or not </...
Save Attachments <ul><li>public static void handleMultipart(Multipart multipart)  </li></ul><ul><li>throws MessagingExcept...
Save Attachments/2 <ul><li>public static void handlePart(Part part)  </li></ul><ul><li>throws MessagingException, IOExcept...
Save Attachments/3 <ul><li>} else if (disposition.equals(Part.ATTACHMENT)) { </li></ul><ul><li>System.out.println(&quot;At...
Debugging <ul><li>Trace commands sent </li></ul><ul><ul><li>session.setDebug(true) </li></ul></ul>
New Mail Notification Events <ul><li>Add MessageCountListener to folder </li></ul><ul><ul><li>Find out when new messages a...
More Notification Events <ul><li>Transport/Store/Folder.addConnectionListener() </li></ul><ul><ul><li>open, closed, discon...
JavaMail Searching <ul><li>API includes support for searching for matching messages </li></ul><ul><li>javax.mail.search pa...
Building Up SearchTerm <ul><li>AND terms (class AndTerm) </li></ul><ul><li>OR terms (class OrTerm) </li></ul><ul><li>NOT t...
Using SearchTerm <ul><li>Folder folder = store.getFolder(&quot;INBOX&quot;); </li></ul><ul><li>SearchTerm st = new AndTerm...
S/MIME <ul><li>Includes email signing and encryption support </li></ul><ul><li>Get a different provider </li></ul><ul><ul>...
JavaMail with JSP <ul><li>Definitely doable with Java source scriptlets </li></ul><ul><li>However, should limit amount of ...
JavaMail with NNTP <ul><li>You can use JavaMail with NNTP </li></ul><ul><li>Need to get an NNTP provider </li></ul><ul><ul...
JavaMail is Free <ul><li>Sun’s reference implementation is completely free </li></ul><ul><li>Sun’s License:  </li></ul><ul...
James <ul><li>Java Apache Mail Enterprise Server </li></ul><ul><li>Pure Java-based mail server </li></ul><ul><li>Supports ...
Miscellaneous <ul><li>Sun’s JavaMail FAQ </li></ul><ul><ul><li>http://java.sun.com/products/javamail/FAQ.html </li></ul></...
Other Providers <ul><li>knife </li></ul><ul><ul><li>http://www.dog.net.uk/knife/ </li></ul></ul><ul><ul><li>NNTP, POP3, ma...
Questions & Answers <ul><li>Questions? </li></ul><ul><ul><li>Use the FAQs </li></ul></ul><ul><li>John Zukowski </li></ul><...
Upcoming SlideShare
Loading in …5
×

香港六合彩 &raquo; SlideShare

1,798 views
1,721 views

Published on

秃子!邱子东没有理会周秃子,掉头走了。香港六合彩要责问杜元潮:凭什么剥夺了香港六合彩的审批权。路上遇到了副镇长吴同干。老邱你回来了?你去哪儿?邱子东看着吴同干提了两塑料桶油,问。吴同干举了举手中的塑料桶,说:杜书记让我去上头要化肥。你去上头要化肥?邱子东不明白了,油麻地跑外交的是香港六合彩呀!杜书记说,以后,你与香港六合彩一起抓全面,原先由你管的这摊事就都分给我来做了。我怎么行呢?我也不像你那样,外头有那么多关系,香港六合彩又笨。邱子东讥讽地一笑:你怎么就不行呢?你行!香港六合彩看着吴同干手中的两塑料桶油,这油是从哪儿打来的?油坊。我对二扣子说过,没有我的批条,谁也不能从油坊里打油,一滴都不行!吴同干笑了:二扣子已不再负责油坊了,二扣子到三队做队长去了,现在是三队队长林一如管油坊,香港六合彩俩正好倒了个个儿。谁的主意?杜书记提议的。吴同干心里惦记着要化肥计划,就往前走去,但走了几步又回来说,窑厂负责香港六合彩也换了,王家宽去六队做队长,六队队长沈国民做窑厂厂长。说完,提着灌得满满的两桶油,迈着阔步,信心十足地走在油麻地通向外面的路上。最终,香港六合彩没有去找杜元潮。香港六合彩双手插在腰间,站在油麻地的田野上,任由风撩起香港六合彩的衣角、吹乱香港六合彩的头发。香港六合彩的嘴角一直挂着冷冷的微笑。傲慢之后,便是一股抵挡不住的虚弱。审批与外交,是香港六合彩得以在油麻地纵横驰骋的双翼。而如今,这双翼被香港六合彩一向不放在心上的杜元潮剪断了,香港六合彩有一种扑腾在泥灰里的无可奈何的感觉。香港六合彩来到这个世界上,一路高扬,一路风光地生活了这么多年头,第一回刻骨铭心地体味到了剥夺一词的含义。这种感觉犹如一枚冷箭穿透了脊椎。香港六合彩觉得油麻地的田野似乎变得空旷起来。虚弱之后,又是傲慢。二傻子在田野上追逐着一条发情的母牛,在嘴中含糊不清地叫唤着。香港六合彩看到了那条母牛的臀部上方所流出来的亮晶晶的黏液。这黏液的气味刺激了香港六合彩,使香港六合彩不顾一切地向母牛扑去。母牛越过一条水渠,向前奔突着。二傻子在母牛越过水渠时,掉进了水渠,半天,才爬了上来。邱子东想到了戴萍,香港六合彩想要香港六合彩,现在就想要。邱子东拼命地与戴萍做爱。长长地做,狠狠地做,花样翻新地做。一次,香港六合彩将小学校的一张课桌整得瘫痪在了地上。白天,戴萍讲课总打不起精神来,学生做作业时,香港六合彩原来是想看着的,但不一会儿就趴在讲台上睡着了。下一堂课的老师都进教室了,香港六合彩还没醒来。邱子东越来越瘦,也越来越慵懒,常常是睡到快中午了,才起床。但,两香港六合彩也越来越觉得没有味道了,尤其是戴萍。一堆火,正在灰暗中一点一点地矮下去。居然有一回,邱子东让香港六合彩在夜晚于草垛下等着香港六合彩时,香港六合彩说:今天,我不怎么想……与此同时,油麻地小学的男教师林文藻正一步一步地向戴萍靠近。林文藻一副很文弱的样子,十指修长,白嫩光滑,会拉一手好胡琴。《天瓢》【下】[第98节]骚雨/痴雨5(1)在收割早稻的时候,采芹回到了油麻地。香港六合彩和杜元潮有过一次约会。

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,798
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

香港六合彩 &raquo; SlideShare

  1. 1. JavaMail API Fundamentals John Zukowski - jaz@zukowski.net JZ Ventures, Inc.
  2. 2. Agenda <ul><li>Getting Started </li></ul><ul><li>Core Classes </li></ul><ul><li>Getting Mail </li></ul><ul><li>Sending Mail </li></ul><ul><li>Attachments </li></ul><ul><li>Searching </li></ul><ul><li>Q&A </li></ul>
  3. 3. Getting Started <ul><li>Without (Pre?) JavaMail </li></ul><ul><li>With JavaMail </li></ul><ul><ul><li>http://java.sun.com/products/javamail/ </li></ul></ul>
  4. 4. Without JavaMail <ul><li>Read RFC 821 for SMTP </li></ul><ul><ul><li>http://www.cis.ohio-state.edu/htbin/rfc/rfc821.html </li></ul></ul><ul><ul><li>Other RFC describe things like attachments </li></ul></ul><ul><ul><ul><li>RFC 822: Text Messages, RFC 2045-7: MIME, RFC 1939: POP3, RFC 2060: IMAP, ... </li></ul></ul></ul><ul><li>Open socket connection to port 25 </li></ul><ul><ul><li>HELO sending host </li></ul></ul><ul><ul><li>MAIL FROM: sender email </li></ul></ul><ul><ul><li>RCPT TO: recipient email </li></ul></ul><ul><ul><li>DATA </li></ul></ul><ul><ul><li>... the email message... </li></ul></ul><ul><ul><li>... any number of lines ... </li></ul></ul><ul><ul><li>. </li></ul></ul><ul><ul><li>QUIT </li></ul></ul>
  5. 5. SmtpClient <ul><li>import sun.net.smtp.SmtpClient; import java.io.PrintStream; </li></ul><ul><li>public class SmtpClientExample { </li></ul><ul><li>public static void main (String args[]) throws Exception { </li></ul><ul><li>String host = args[0]; </li></ul><ul><li>String from = args[1]; </li></ul><ul><li>String to = args[2]; </li></ul><ul><li>SmtpClient smtp = new SmtpClient(host); </li></ul><ul><li>smtp.from(from); </li></ul><ul><li>smtp.to(to); </li></ul><ul><li>PrintStream msg = smtp.startMessage(); </li></ul><ul><li>msg.println(&quot;To: &quot; + to); </li></ul><ul><li>msg.println(&quot;Subject: Hello SmtpClient&quot;); </li></ul><ul><li>msg.println(); // blank line between headers and message </li></ul><ul><li>msg.println(&quot;This is a test message.&quot;); </li></ul><ul><li>smtp.closeServer(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  6. 6. Without JavaMail Take 3 <ul><li>Just use a URL </li></ul><ul><li>Open a mailto: URL </li></ul><ul><li>Write to opened URLConnection </li></ul><ul><li>Need to set mail.host System property </li></ul><ul><li>Be sure to end lines with </li></ul><ul><ul><li>Don’t use println() </li></ul></ul>
  7. 7. Mail Through URL <ul><li>import java.io.*; </li></ul><ul><li>import java.net.*; </li></ul><ul><li>public class MailTo { </li></ul><ul><li>public static void main(String args[]) throws Exception { </li></ul><ul><li>System.getProperties().put(&quot;mail.host&quot;, args[0]); </li></ul><ul><li>URL url = new URL(&quot;mailto:jaz@jguru.com&quot;); </li></ul><ul><li>URLConnection conn = url.openConnection(); </li></ul><ul><li>PrintStream out = new </li></ul><ul><li>PrintStream(conn.getOutputStream(), true); </li></ul><ul><li>out.print(&quot;From: jguru-fan@yourisp.com&quot;+&quot; &quot;); </li></ul><ul><li>out.print(&quot;Subject: Works Great!&quot;+&quot; &quot;); </li></ul><ul><li>out.print(&quot;Thanks!&quot;+&quot; &quot;); </li></ul><ul><li>out.close(); </li></ul><ul><li>System.out.println(&quot;Message Sent&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. With JavaMail API <ul><li>Latest Version 1.2 </li></ul><ul><ul><li>December 5, 2000 release </li></ul></ul><ul><ul><li>Sun includes IMAP, POP, and SMTP service providers </li></ul></ul><ul><ul><li>Version 1.1.3 (2/22/2000) most popular one used </li></ul></ul><ul><li>Need to get JavaBeans Activation Framework </li></ul><ul><ul><li>http://java.sun.com/beans/glasgow/jaf.html </li></ul></ul><ul><li>See demo directory for many examples </li></ul>
  9. 9. JavaMail Setup <ul><li>Add JAR files </li></ul><ul><ul><li>to CLASSPATH, to jre/lib/ext </li></ul></ul><ul><ul><li>Applets can use: javax.* </li></ul></ul><ul><ul><ul><li>Won’t work in Netscape though without Plug-in </li></ul></ul></ul><ul><ul><ul><li>Will work in Internet Explorer / HotJava </li></ul></ul></ul><ul><li>JavaMail </li></ul><ul><ul><li>mail.jar (280K) </li></ul></ul><ul><ul><li>Separate JAR files available if only using parts </li></ul></ul><ul><li>Activation Framework </li></ul><ul><ul><li>activation.jar (45K) </li></ul></ul>
  10. 10. JDK Requirements <ul><li>Works with JDK 1.1.6+ </li></ul><ul><li>Works with Java 2 platform, Standard Edition, versions 1.2 / 1.3 </li></ul><ul><li>Included with Java 2 platform, Enterprise Edition </li></ul>
  11. 11. Core Classes <ul><li>Session </li></ul><ul><li>Message / MimeMessage </li></ul><ul><li>InternetAddress </li></ul><ul><li>Authenticator </li></ul><ul><li>Transport </li></ul>
  12. 12. Session <ul><li>Represents a mail session </li></ul><ul><li>Uses Properties to get things like mail host </li></ul><ul><ul><li>mail.host </li></ul></ul><ul><ul><li>mail.smtp.host </li></ul></ul><ul><li>Get session - no constructor </li></ul><ul><ul><li>Session session = Session.getInstance(props, null); // null for Authenticator </li></ul></ul><ul><ul><li>Session session = Session.getDefaultInstance(props, null); </li></ul></ul>
  13. 13. Message / MimeMessage <ul><li>Represents a mail message </li></ul><ul><ul><li>Message abstract class </li></ul></ul><ul><ul><li>implements Part </li></ul></ul><ul><ul><li>MimeMessage is MIME style email message </li></ul></ul><ul><ul><li>implements MimePart </li></ul></ul><ul><li>Get message from session </li></ul><ul><ul><li>MimeMessage message = new MimeMessage(session); </li></ul></ul><ul><li>Set parts </li></ul><ul><ul><li>message.setContent() / mimeMessage.setText() </li></ul></ul>
  14. 14. InternetAddress <ul><li>RFC822 Address </li></ul><ul><li>Create: </li></ul><ul><ul><li>new InternetAddress(&quot;jaz@zukowski.net&quot;); </li></ul></ul><ul><ul><li>new InternetAddress(&quot;jaz@zukowski.net &quot;, &quot;John Zukowski&quot;); </li></ul></ul><ul><li>For To, From, CC, BCC </li></ul><ul><ul><li>message.setFrom(address) </li></ul></ul><ul><ul><li>message.addRecipient(type, address) </li></ul></ul><ul><ul><li>Types </li></ul></ul><ul><ul><ul><li>Message.RecipientType.TO </li></ul></ul></ul><ul><ul><ul><li>Message.RecipientType.CC </li></ul></ul></ul><ul><ul><ul><li>Message.RecipientType.BCC </li></ul></ul></ul>
  15. 15. Authenticator <ul><li>Permit mechanism to prompt for username and password </li></ul><ul><ul><li>javax.mail.Authenticator != java.net.Authenticator </li></ul></ul><ul><li>Extend Authenticator </li></ul><ul><li>Override: </li></ul><ul><ul><li>public PasswordAuthentication getPasswordAuthentication() { </li></ul></ul><ul><ul><li>String username, password; // Then get them ... </li></ul></ul><ul><ul><li>return new PasswordAuthentication(username, password); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  16. 16. Transport <ul><li>Message transport mechanism </li></ul><ul><li>Get transport for session </li></ul><ul><ul><li>Transport transport = session.getTransport(&quot;smtp&quot;); </li></ul></ul><ul><li>Connect </li></ul><ul><ul><li>transport.connect(host, username, password); </li></ul></ul><ul><li>Act - repeat if necessary </li></ul><ul><ul><li>transport.sendMessage(message, message.getAllRecipients()); </li></ul></ul><ul><li>Done </li></ul><ul><ul><li>transport.close(); </li></ul></ul>
  17. 17. Sending Mail <ul><li>Need a working SMTP server </li></ul><ul><ul><li>Can be written in Java using JavaMail API, but irrelevant </li></ul></ul><ul><ul><li>Need from/to addresses, but don’t need to be valid, unless SMTP server includes some form of verification </li></ul></ul><ul><li>To run example: </li></ul><ul><ul><li>java MailExample smtp.mailserver from@from.com to@to.com </li></ul></ul>
  18. 18. Hello World <ul><li>import java.util.Properties; </li></ul><ul><li>import javax.mail.*; </li></ul><ul><li>import javax.mail.internet.*; </li></ul><ul><li>public class MailExample { </li></ul><ul><li>public static void main (String args[]) throws Exception { </li></ul><ul><li>String host = args[0]; </li></ul><ul><li>String from = args[1]; </li></ul><ul><li>String to = args[2]; </li></ul><ul><li>// Get system properties </li></ul><ul><li>Properties props = System.getProperties(); </li></ul><ul><li>// Setup mail server </li></ul><ul><li>props.put(&quot;mail.smtp.host&quot;, host); </li></ul>
  19. 19. Hello World/2 <ul><li>// Get session </li></ul><ul><li>Session session = Session.getInstance(props, null); </li></ul><ul><li>// Define message </li></ul><ul><li>MimeMessage message = new MimeMessage(session); </li></ul><ul><li>message.setFrom(new InternetAddress(from)); </li></ul><ul><li>message.addRecipient(Message.RecipientType.TO, </li></ul><ul><li>new InternetAddress(to)); </li></ul><ul><li>message.setSubject(&quot;Hello JavaMail&quot;); </li></ul><ul><li>message.setText(&quot;Welcome to JavaMail&quot;); </li></ul><ul><li>// Send message </li></ul><ul><li>Transport.send(message); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 20. Getting Mail <ul><li>POP3 provider doesn’t provide local data storage </li></ul><ul><li>There are mailbox store providers available </li></ul><ul><li>Need to get/install POP3 provider </li></ul><ul><ul><li>Part of JavaMail 1.2 release </li></ul></ul><ul><ul><li>NOT part of basic JavaMail 1.1 API implementation </li></ul></ul>
  21. 21. Reading Hello World <ul><li>import java.io.*; </li></ul><ul><li>import java.util.Properties; </li></ul><ul><li>import javax.mail.*; </li></ul><ul><li>import javax.mail.internet.*; </li></ul><ul><li>public class GetMessageExample { </li></ul><ul><li>public static void main (String args[]) throws Exception { </li></ul><ul><li>String host = args[0]; </li></ul><ul><li>String username = args[1]; </li></ul><ul><li>String password = args[2]; </li></ul><ul><li>// Create empty properties </li></ul><ul><li>Properties props = new Properties(); </li></ul><ul><li>// Get session </li></ul><ul><li>Session session = Session.getInstance(props, null); </li></ul>
  22. 22. Reading Hello World/2 <ul><li>// Get the store </li></ul><ul><li>Store store = session.getStore(&quot;pop3&quot;); </li></ul><ul><li>store.connect(host, username, password); </li></ul><ul><li>// Get folder </li></ul><ul><li>Folder folder = store.getFolder(&quot;INBOX&quot;); </li></ul><ul><li>folder.open(Folder.READ_ONLY); </li></ul><ul><li>BufferedReader reader = new BufferedReader ( </li></ul><ul><li>new InputStreamReader(System.in)); </li></ul><ul><li>// Get directory </li></ul><ul><li>Message message[] = folder.getMessages(); </li></ul><ul><li>for (int i=0, n=message.length; i<n; i++) { </li></ul>
  23. 23. Reading Hello World/3 <ul><li>System.out.println(i + &quot;: &quot; + message[i].getFrom()[0] </li></ul><ul><li>+ &quot; &quot; + message[i].getSubject()); </li></ul><ul><li>System.out.println(&quot;Do you want to read message? [YES to read/QUIT to end]&quot;); </li></ul><ul><li>String line = reader.readLine(); </li></ul><ul><li>if (&quot;YES&quot;.equals(line)) { </li></ul><ul><li>message[i].writeTo(System.out); </li></ul><ul><li>} else if (&quot;QUIT&quot;.equals(line)) { </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>// Close connection </li></ul><ul><li>folder.close(false); </li></ul><ul><li>store.close(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 24. Authenticator Usage <ul><li>Put host in properties </li></ul><ul><ul><li>Properties props = new Properties(); </li></ul></ul><ul><ul><li>props.put(&quot;mail.host&quot;, host); </li></ul></ul><ul><li>Setup authentication, get session </li></ul><ul><ul><li>Authenticator auth = new PopupAuthenticator(); </li></ul></ul><ul><ul><li>Session session = Session.getInstance(props, auth); </li></ul></ul><ul><li>Get the store </li></ul><ul><ul><li>Store store = session.getStore(&quot;pop3&quot;); </li></ul></ul><ul><ul><li>store.connect(); </li></ul></ul>
  25. 25. Saving Messages <ul><li>To save copy locally: </li></ul><ul><ul><li>Get/create appropriate provider </li></ul></ul><ul><ul><ul><li>Knife - http://www.dog.net.uk/knife/ </li></ul></ul></ul><ul><ul><li>mimeMessage.writeTo(outputStream) </li></ul></ul>
  26. 26. Replying <ul><li>Use Message.reply(boolean) </li></ul><ul><ul><li>Sets up message with proper headers </li></ul></ul><ul><ul><li>boolean of true indicates reply to all vs. reply to sender only </li></ul></ul><ul><ul><li>Does NOT setup message contents </li></ul></ul>
  27. 27. Deleting Messages <ul><li>Set message flag to deleted: </li></ul><ul><ul><li>message.setFlag(Flags.Flag.DELETED, true); </li></ul></ul><ul><li>Be sure folder opened read-write: </li></ul><ul><ul><li>folder.open(Folder.READ_WRITE); </li></ul></ul><ul><li>Deleted when folder closed: </li></ul><ul><ul><li>folder.close(true); // true = expunge </li></ul></ul><ul><li>Expunge / Permanently Deletes </li></ul><ul><ul><li>folder.expunge() </li></ul></ul><ul><ul><li>NOT always implemented </li></ul></ul>
  28. 28. Including Attachments <ul><li>Each attachment goes into a MimeBodyPart </li></ul><ul><li>DataHandler deals with reading in contents </li></ul><ul><ul><li>Provide it with a DataSource </li></ul></ul><ul><ul><li>Either URLDataSource or FileDataSource </li></ul></ul>
  29. 29. Sending Attachments <ul><li>// create mime message object and set the required parameters </li></ul><ul><li>MimeMessage message = createMessage(to, cc, subject); </li></ul><ul><li>// create the message part </li></ul><ul><li>MimeBodyPart messageBodyPart = new MimeBodyPart(); </li></ul><ul><li>//fill message </li></ul><ul><li>messageBodyPart.setText(msg); </li></ul><ul><li>Multipart multipart = new MimeMultipart(); </li></ul><ul><li>multipart.addBodyPart(messageBodyPart); </li></ul><ul><li>// fill the array of files to be attached </li></ul><ul><li>File [] attachments = { .... } </li></ul>
  30. 30. Sending Attachments/2 <ul><li>for( int i = 0; i < attachments.length; i++ ) { </li></ul><ul><li>messageBodyPart = new MimeBodyPart(); </li></ul><ul><li>FileDataSource fileDataSource =new FileDataSource(attachments[i]); </li></ul><ul><li>messageBodyPart.setDataHandler(new DataHandler(fileDataSource)); </li></ul><ul><li>messageBodyPart.setFileName(attachments[i].getName()); </li></ul><ul><li>multipart.addBodyPart(messageBodyPart); </li></ul><ul><li>} </li></ul><ul><li>// add the Multipart to the message </li></ul><ul><li>message.setContent(multipart); </li></ul><ul><li>// SEND THE MESSAGE </li></ul><ul><li>Transport.send(message); </li></ul>
  31. 31. Sending HTML Attachment <ul><li>Don’t use setText() </li></ul><ul><li>Use setDataHandler() </li></ul><ul><ul><li>String htmlText = &quot;<H1>Hello</H1><H2>World</H2>&quot;; </li></ul></ul><ul><ul><li>message.setContent(htmlText, &quot;text/html&quot;)); </li></ul></ul>
  32. 32. Including HTML Images Inline <ul><li>Specify Image source with cid: URL </li></ul><ul><ul><li><IMG SRC=cid:23abc@pc27> </li></ul></ul><ul><li>Set Content-ID in header of image attachment </li></ul><ul><ul><li>part.setHeader(&quot;Content-ID&quot;,&quot;23abc@pc27&quot;); </li></ul></ul><ul><li>Complete example: </li></ul><ul><ul><li>http://www.jguru.com/jguru/faq/view.jsp?EID=97371 </li></ul></ul>
  33. 33. Getting Attachments from Client <ul><li>You want to create a web-based email system </li></ul><ul><li>Your user wants to include file from their system as attachment </li></ul><ul><li>Use an HTML Form </li></ul><ul><li>Use Servlet to read stream </li></ul><ul><li>Use MultipartRequest class from O’Reilly servlets book – www.servlets.com </li></ul>
  34. 34. HTML Form <ul><li><FORM ENCTYPE=&quot;multipart/form-data&quot; method=post action=&quot;/myservlet&quot;> </li></ul><ul><li><INPUT TYPE=&quot;file&quot; NAME=&quot;mptest&quot;> </li></ul><ul><li><INPUT TYPE=&quot;submit&quot; VALUE=&quot;upload&quot;> </li></ul><ul><li></FORM> </li></ul>
  35. 35. Getting Attachments <ul><li>For each part of Multipart, process part </li></ul><ul><li>Attachments can be inline or not </li></ul><ul><ul><li>String disposition = part.getDisposition(); </li></ul></ul><ul><ul><li>if (disposition.equals(Part.INLINE)) </li></ul></ul><ul><ul><li>if (disposition.equals(Part.ATTACHMENT)) </li></ul></ul>
  36. 36. Save Attachments <ul><li>public static void handleMultipart(Multipart multipart) </li></ul><ul><li>throws MessagingException, IOException { </li></ul><ul><li>for (int i=0, n=multipart.getCount(); i<n; i++) { </li></ul><ul><li>handlePart(multipart.getBodyPart(i)); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Following code doesn’t deal with Multipart in Multipart </li></ul><ul><ul><li>left as exercise for reader  </li></ul></ul>
  37. 37. Save Attachments/2 <ul><li>public static void handlePart(Part part) </li></ul><ul><li>throws MessagingException, IOException { </li></ul><ul><li>String disposition = part.getDisposition(); </li></ul><ul><li>String contentType = part.getContentType(); </li></ul><ul><li>if (disposition == null) { // When just body </li></ul><ul><li>System.out.println(&quot;Null: &quot; + contentType); </li></ul><ul><li>// Check if plain </li></ul><ul><li>if ((contentType.length() >= 10) && </li></ul><ul><li>(contentType.toLowerCase().substring(0, 10).equals(&quot;text/plain&quot;))) { </li></ul><ul><li>part.writeTo(System.out); </li></ul><ul><li>} else { // Don't think this will happen </li></ul><ul><li>System.out.println(&quot;Other body: &quot; + contentType); </li></ul><ul><li>part.writeTo(System.out); </li></ul><ul><li>} </li></ul>
  38. 38. Save Attachments/3 <ul><li>} else if (disposition.equals(Part.ATTACHMENT)) { </li></ul><ul><li>System.out.println(&quot;Attachment: &quot; + part.getFileName() + </li></ul><ul><li>&quot; : &quot; + contentType); </li></ul><ul><li>saveFile(part.getFileName(), part.getInputStream()); </li></ul><ul><li>} else if (disposition.equals(Part.INLINE)) { </li></ul><ul><li>System.out.println(&quot;Inline: &quot; + part.getFileName() + </li></ul><ul><li>&quot; : &quot; + contentType); </li></ul><ul><li>saveFile(part.getFileName(), part.getInputStream()); </li></ul><ul><li>} else { // Should never happen </li></ul><ul><li>System.out.println(&quot;Other: &quot; + disposition); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  39. 39. Debugging <ul><li>Trace commands sent </li></ul><ul><ul><li>session.setDebug(true) </li></ul></ul>
  40. 40. New Mail Notification Events <ul><li>Add MessageCountListener to folder </li></ul><ul><ul><li>Find out when new messages are received </li></ul></ul><ul><ul><li>Sleep then folder.getMessageCount() to get notification from IMAP server </li></ul></ul><ul><ul><li>Not POP3 - Does not work when folder open </li></ul></ul>
  41. 41. More Notification Events <ul><li>Transport/Store/Folder.addConnectionListener() </li></ul><ul><ul><li>open, closed, disconnected </li></ul></ul><ul><li>Folder.addFolderListener() </li></ul><ul><ul><li>created, deleted, renamed </li></ul></ul><ul><li>Folder.addMessageChangeListener </li></ul><ul><ul><li>changed </li></ul></ul><ul><li>Store.addStoreListener </li></ul><ul><ul><li>notification </li></ul></ul><ul><li>Transport.addTransportListener </li></ul><ul><ul><li>message delivered, not delivered, partially delivered </li></ul></ul>
  42. 42. JavaMail Searching <ul><li>API includes support for searching for matching messages </li></ul><ul><li>javax.mail.search package </li></ul><ul><li>Build a SearchTerm </li></ul><ul><li>Search: </li></ul><ul><ul><li>Message[] msgs = folder.search(st); </li></ul></ul>
  43. 43. Building Up SearchTerm <ul><li>AND terms (class AndTerm) </li></ul><ul><li>OR terms (class OrTerm) </li></ul><ul><li>NOT terms (class NotTerm) </li></ul><ul><li>SENT DATE terms (class SentDateTerm) </li></ul><ul><li>CONTENT terms (class BodyTerm) </li></ul><ul><li>HEADER terms (FromTerm / FromStringTerm, RecipientTerm / RecipientStringTerm, SubjectTerm, etc..) </li></ul>
  44. 44. Using SearchTerm <ul><li>Folder folder = store.getFolder(&quot;INBOX&quot;); </li></ul><ul><li>SearchTerm st = new AndTerm(new SubjectTerm(&quot;ADV:&quot;), new BodyTerm(&quot;hello&quot;); </li></ul><ul><li>Message[] msgs = folder.search(st); </li></ul>
  45. 45. S/MIME <ul><li>Includes email signing and encryption support </li></ul><ul><li>Get a different provider </li></ul><ul><ul><li>Phaos S/MIME toolkit </li></ul></ul><ul><ul><ul><li>http://www.phaos.com/e_security/prod_smime.html </li></ul></ul></ul><ul><ul><li>JCSI </li></ul></ul><ul><ul><ul><li>http://security.dstc.edu.au/projects/java/release2.html </li></ul></ul></ul>
  46. 46. JavaMail with JSP <ul><li>Definitely doable with Java source scriptlets </li></ul><ul><li>However, should limit amount of Java source in JSP pages </li></ul><ul><li>Use JavaBeans that hide/simplify capabilities for Web designer </li></ul><ul><li>Create / Get </li></ul><ul><ul><li>ImMailBean </li></ul></ul><ul><ul><ul><li>http://www.imessaging.com/html/immailbean.html </li></ul></ul></ul><ul><ul><li>Source Fourge </li></ul></ul><ul><ul><ul><li>http://sourceforge.net/project/?group_id=1282 </li></ul></ul></ul>
  47. 47. JavaMail with NNTP <ul><li>You can use JavaMail with NNTP </li></ul><ul><li>Need to get an NNTP provider </li></ul><ul><ul><li>http://www.dog.net.uk/knife/ </li></ul></ul><ul><li>To read newsgroups </li></ul><ul><ul><li>Store store = session.getStore(&quot;nntp&quot;); </li></ul></ul><ul><ul><li>store.connect(host, username, password); </li></ul></ul><ul><ul><li>Folder folder = store.getFolder(newsgroup); </li></ul></ul><ul><ul><li>folder.open(Folder.READ_ONLY); </li></ul></ul><ul><ul><li>Message message[] = folder.getMessages(); </li></ul></ul>
  48. 48. JavaMail is Free <ul><li>Sun’s reference implementation is completely free </li></ul><ul><li>Sun’s License: </li></ul><ul><ul><li>http://java.sun.com/products/javamail/LICENSE.txt </li></ul></ul><ul><li>Includes SMTP, IMAP, and POP3 providers </li></ul>
  49. 49. James <ul><li>Java Apache Mail Enterprise Server </li></ul><ul><li>Pure Java-based mail server </li></ul><ul><li>Supports Mailets </li></ul><ul><ul><li>Like servlets, but for extending mail server </li></ul></ul><ul><ul><li>Add capabilities like mailing list support, filtering, translation, etc. </li></ul></ul><ul><li>http://java.apache.org/james/index.html </li></ul>
  50. 50. Miscellaneous <ul><li>Sun’s JavaMail FAQ </li></ul><ul><ul><li>http://java.sun.com/products/javamail/FAQ.html </li></ul></ul><ul><li>Mailing List </li></ul><ul><ul><li>http://archives.java.sun.com/archives/javamail-interest.html </li></ul></ul><ul><li>Get the JavaMail Source </li></ul><ul><ul><li>1.1.2 source part of J2EE Sun Community Source Licensing </li></ul></ul><ul><ul><li>http://www.sun.com/software/communitysource/j2ee/ </li></ul></ul>
  51. 51. Other Providers <ul><li>knife </li></ul><ul><ul><li>http://www.dog.net.uk/knife/ </li></ul></ul><ul><ul><li>NNTP, POP3, mailbox file provider </li></ul></ul><ul><li>Project &quot;POPpers&quot; </li></ul><ul><ul><li>http://www2s.biglobe.ne.jp/~dat/java/project/poppers/index_en.html </li></ul></ul><ul><li>ICEMail </li></ul><ul><ul><li>Java-based Email Client </li></ul></ul><ul><ul><li>http://www.icemail.org/ </li></ul></ul>
  52. 52. Questions & Answers <ul><li>Questions? </li></ul><ul><ul><li>Use the FAQs </li></ul></ul><ul><li>John Zukowski </li></ul><ul><ul><li>http://www.jguru.com </li></ul></ul><ul><ul><li>http://java.about.com </li></ul></ul><ul><ul><li>http://www.jguru.com/faq/JavaMail </li></ul></ul>

×