虽然jakarta的commons email 简化了javamail的使用,但是遇到复杂一点的东东,我们还是需要重新拿起javamail来,也许我们可以做.

其实javamail也不是太复杂 o_o 下面是通过gmail发送邮件,因为gmail需要smtp验证,所有要额外的设定
mail.smtp.auth 值为 true
并且添加
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

我们来看一下刘冬的代码:
http://www.javayou.com/showlog<wbr>.jspe?log_id=498

1. 邮件收取

 1None.gifpackage lius.javamail.ssl;
 2None.gif
 3None.gifimport java.io.UnsupportedEncodingException;
 4None.gifimport java.security.*;
 5None.gifimport java.util.Properties;
 6None.gifimport javax.mail.*;
 7None.gifimport javax.mail.internet.InternetAddress;
 8None.gifimport javax.mail.internet.MimeUtility;
 9None.gif
10ExpandedBlockStart.gifContractedBlock.gif/** *//**
11InBlock.gif * 用于收取Gmail邮件
12InBlock.gif * @author Winter Lau
13ExpandedBlockEnd.gif */

14ExpandedBlockStart.gifContractedBlock.gifpublic class GmailFetch dot.gif{
15InBlock.gif 
16ExpandedSubBlockStart.gifContractedSubBlock.gif public static void main(String argv[]) throws Exception dot.gif{
17InBlock.gif
18InBlock.gif  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
19InBlock.gif  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
20InBlock.gif
21InBlock.gif  // Get a Properties object
22InBlock.gif  Properties props = System.getProperties();
23InBlock.gif  props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
24InBlock.gif  props.setProperty("mail.pop3.socketFactory.fallback""false");
25InBlock.gif  props.setProperty("mail.pop3.port""995");
26InBlock.gif  props.setProperty("mail.pop3.socketFactory.port""995");
27InBlock.gif
28InBlock.gif  //以下步骤跟一般的JavaMail操作相同
29InBlock.gif  Session session = Session.getDefaultInstance(props,null);
30InBlock.gif
31InBlock.gif  //请将红色部分对应替换成你的邮箱帐号和密码
32InBlock.gif  URLName urln = new URLName("pop3","pop.gmail.com",995,null,
33InBlock.gif    "[邮箱帐号]""[邮箱密码]");
34InBlock.gif  Store store = session.getStore(urln);
35InBlock.gif  Folder inbox = null;
36ExpandedSubBlockStart.gifContractedSubBlock.gif  try dot.gif{
37InBlock.gif   store.connect();
38InBlock.gif   inbox = store.getFolder("INBOX");
39InBlock.gif   inbox.open(Folder.READ_ONLY);
40InBlock.gif   FetchProfile profile = new FetchProfile();
41InBlock.gif   profile.add(FetchProfile.Item.ENVELOPE);
42InBlock.gif   Message[] messages = inbox.getMessages();
43InBlock.gif   inbox.fetch(messages, profile);
44InBlock.gif   System.out.println("收件箱的邮件数:" + messages.length);
45ExpandedSubBlockStart.gifContractedSubBlock.gif   for (int i = 0; i < messages.length; i++dot.gif{
46InBlock.gif    //邮件发送者
47InBlock.gif    String from = decodeText(messages[i].getFrom()[0].toString());
48InBlock.gif    InternetAddress ia = new InternetAddress(from);
49InBlock.gif    System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
50InBlock.gif    //邮件标题
51InBlock.gif    System.out.println("TITLE:" + messages[i].getSubject());
52InBlock.gif    //邮件大小
53InBlock.gif    System.out.println("SIZE:" + messages[i].getSize());
54InBlock.gif    //邮件发送时间
55InBlock.gif    System.out.println("DATE:" + messages[i].getSentDate());
56ExpandedSubBlockEnd.gif   }

57ExpandedSubBlockStart.gifContractedSubBlock.gif  }
 finally dot.gif{
58ExpandedSubBlockStart.gifContractedSubBlock.gif   try dot.gif{
59InBlock.gif    inbox.close(false);
60ExpandedSubBlockStart.gifContractedSubBlock.gif   }
 catch (Exception e) dot.gif{}
61ExpandedSubBlockStart.gifContractedSubBlock.gif   try dot.gif{
62InBlock.gif    store.close();
63ExpandedSubBlockStart.gifContractedSubBlock.gif   }
 catch (Exception e) dot.gif{}
64ExpandedSubBlockEnd.gif  }

65ExpandedSubBlockEnd.gif }

66InBlock.gif 
67InBlock.gif protected static String decodeText(String text)
68ExpandedSubBlockStart.gifContractedSubBlock.gif   throws UnsupportedEncodingException dot.gif{
69InBlock.gif  if (text == null)
70InBlock.gif   return null;
71InBlock.gif  if (text.startsWith("=?GB"|| text.startsWith("=?gb"))
72InBlock.gif   text = MimeUtility.decodeText(text);
73InBlock.gif  else
74InBlock.gif   text = new String(text.getBytes("ISO8859_1"));
75InBlock.gif  return text;
76ExpandedSubBlockEnd.gif }

77InBlock.gif
78ExpandedBlockEnd.gif}

2. 发送邮件
 1 None.gif package  lius.javamail.ssl;
 2 None.gif
 3 None.gif import  java.security.Security;
 4 None.gif import  java.util.Date;
 5 None.gif import  java.util.Properties;
 6 None.gif
 7 None.gif import  javax.mail.Authenticator;
 8 None.gif import  javax.mail.Message;
 9 None.gif import  javax.mail.MessagingException;
10 None.gif import  javax.mail.PasswordAuthentication;
11 None.gif import  javax.mail.Session;
12 None.gif import  javax.mail.Transport;
13 None.gif import  javax.mail.internet.AddressException;
14 None.gif import  javax.mail.internet.InternetAddress;
15 None.gif import  javax.mail.internet.MimeMessage;
16 None.gif
17 ExpandedBlockStart.gifContractedBlock.gif /** */ /**
18InBlock.gif * 使用Gmail发送邮件
19InBlock.gif * @author Winter Lau
20ExpandedBlockEnd.gif */

21 ExpandedBlockStart.gifContractedBlock.gif public   class  GmailSender  dot.gif {
22InBlock.gif
23ExpandedSubBlockStart.gifContractedSubBlock.gif public static void main(String[] args) throws AddressException, MessagingException dot.gif{
24InBlock.gif  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
25InBlock.gif  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
26InBlock.gif  // Get a Properties object
27InBlock.gif  Properties props = System.getProperties();
28InBlock.gif  props.setProperty("mail.smtp.host""smtp.gmail.com");
29InBlock.gif  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
30InBlock.gif  props.setProperty("mail.smtp.socketFactory.fallback""false");
31InBlock.gif  props.setProperty("mail.smtp.port""465");
32InBlock.gif  props.setProperty("mail.smtp.socketFactory.port""465");
33InBlock.gif  props.put("mail.smtp.auth""true");
34InBlock.gif  final String username = "[邮箱帐号]";
35InBlock.gif  final String password = "[邮箱密码]";
36ExpandedSubBlockStart.gifContractedSubBlock.gif  Session session = Session.getDefaultInstance(props, new Authenticator()dot.gif{
37ExpandedSubBlockStart.gifContractedSubBlock.gif      protected PasswordAuthentication getPasswordAuthentication() dot.gif{
38InBlock.gif          return new PasswordAuthentication(username, password);
39ExpandedSubBlockEnd.gif      }
}
);
40InBlock.gif
41InBlock.gif       // -- Create a new message --
42InBlock.gif  Message msg = new MimeMessage(session);
43InBlock.gif
44InBlock.gif  // -- Set the FROM and TO fields --
45InBlock.gif  msg.setFrom(new InternetAddress(username + "@mo168.com"));
46InBlock.gif  msg.setRecipients(Message.RecipientType.TO, 
47InBlock.gif    InternetAddress.parse("[收件人地址]",false));
48InBlock.gif  msg.setSubject("Hello");
49InBlock.gif  msg.setText("How are you");
50InBlock.gif  msg.setSentDate(new Date());
51InBlock.gif  Transport.send(msg);
52InBlock.gif  
53InBlock.gif  System.out.println("Message sent.");
54ExpandedSubBlockEnd.gif }

55ExpandedBlockEnd.gif}



关于邮件的解析请看 http://www.javayou.com/showlog.jspe?log_id=372
更多关于javamail的文章 http://www.javayou.com/main.jspe?query=javamail


end