虽然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. 邮件收取
1
package lius.javamail.ssl;
2![None.gif](/Images/OutliningIndicators/None.gif)
3
import java.io.UnsupportedEncodingException;
4
import java.security.*;
5
import java.util.Properties;
6
import javax.mail.*;
7
import javax.mail.internet.InternetAddress;
8
import javax.mail.internet.MimeUtility;
9![None.gif](/Images/OutliningIndicators/None.gif)
10![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
11
* 用于收取Gmail邮件
12
* @author Winter Lau
13
*/
14![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class GmailFetch
{
15
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String argv[]) throws Exception
{
17![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
19
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
20![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
// Get a Properties object
22
Properties props = System.getProperties();
23
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
24
props.setProperty("mail.pop3.socketFactory.fallback", "false");
25
props.setProperty("mail.pop3.port", "995");
26
props.setProperty("mail.pop3.socketFactory.port", "995");
27![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
//以下步骤跟一般的JavaMail操作相同
29
Session session = Session.getDefaultInstance(props,null);
30![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
//请将红色部分对应替换成你的邮箱帐号和密码
32
URLName urln = new URLName("pop3","pop.gmail.com",995,null,
33
"[邮箱帐号]", "[邮箱密码]");
34
Store store = session.getStore(urln);
35
Folder inbox = null;
36![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
37
store.connect();
38
inbox = store.getFolder("INBOX");
39
inbox.open(Folder.READ_ONLY);
40
FetchProfile profile = new FetchProfile();
41
profile.add(FetchProfile.Item.ENVELOPE);
42
Message[] messages = inbox.getMessages();
43
inbox.fetch(messages, profile);
44
System.out.println("收件箱的邮件数:" + messages.length);
45![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < messages.length; i++)
{
46
//邮件发送者
47
String from = decodeText(messages[i].getFrom()[0].toString());
48
InternetAddress ia = new InternetAddress(from);
49
System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
50
//邮件标题
51
System.out.println("TITLE:" + messages[i].getSubject());
52
//邮件大小
53
System.out.println("SIZE:" + messages[i].getSize());
54
//邮件发送时间
55
System.out.println("DATE:" + messages[i].getSentDate());
56
}
57![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} finally
{
58![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
59
inbox.close(false);
60![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{}
61![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
62
store.close();
63![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{}
64
}
65
}
66
67
protected static String decodeText(String text)
68![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
69
if (text == null)
70
return null;
71
if (text.startsWith("=?GB") || text.startsWith("=?gb"))
72
text = MimeUtility.decodeText(text);
73
else
74
text = new String(text.getBytes("ISO8859_1"));
75
return text;
76
}
77![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
78
}
2. 发送邮件
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![None.gif](/Images/OutliningIndicators/None.gif)
7
![None.gif](/Images/OutliningIndicators/None.gif)
8
![None.gif](/Images/OutliningIndicators/None.gif)
9
![None.gif](/Images/OutliningIndicators/None.gif)
10
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
15
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
17
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
21
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
37
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
48
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
54
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
56
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
58
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
59
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
60
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
61
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
62
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
63
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
64
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
67
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
68
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
69
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
70
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
71
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
72
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
73
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
74
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
75
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
76
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
77
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
78
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2. 发送邮件
1
package
lius.javamail.ssl;
2![None.gif](/Images/OutliningIndicators/None.gif)
3
import
java.security.Security;
4
import
java.util.Date;
5
import
java.util.Properties;
6![None.gif](/Images/OutliningIndicators/None.gif)
7
import
javax.mail.Authenticator;
8
import
javax.mail.Message;
9
import
javax.mail.MessagingException;
10
import
javax.mail.PasswordAuthentication;
11
import
javax.mail.Session;
12
import
javax.mail.Transport;
13
import
javax.mail.internet.AddressException;
14
import
javax.mail.internet.InternetAddress;
15
import
javax.mail.internet.MimeMessage;
16![None.gif](/Images/OutliningIndicators/None.gif)
17![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** */
/**
18
* 使用Gmail发送邮件
19
* @author Winter Lau
20
*/
21![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public
class
GmailSender
{
22![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) throws AddressException, MessagingException
{
24
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
25
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
26
// Get a Properties object
27
Properties props = System.getProperties();
28
props.setProperty("mail.smtp.host", "smtp.gmail.com");
29
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
30
props.setProperty("mail.smtp.socketFactory.fallback", "false");
31
props.setProperty("mail.smtp.port", "465");
32
props.setProperty("mail.smtp.socketFactory.port", "465");
33
props.put("mail.smtp.auth", "true");
34
final String username = "[邮箱帐号]";
35
final String password = "[邮箱密码]";
36![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Session session = Session.getDefaultInstance(props, new Authenticator()
{
37![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected PasswordAuthentication getPasswordAuthentication()
{
38
return new PasswordAuthentication(username, password);
39
}});
40![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
// -- Create a new message --
42
Message msg = new MimeMessage(session);
43![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
// -- Set the FROM and TO fields --
45
msg.setFrom(new InternetAddress(username + "@mo168.com"));
46
msg.setRecipients(Message.RecipientType.TO,
47
InternetAddress.parse("[收件人地址]",false));
48
msg.setSubject("Hello");
49
msg.setText("How are you");
50
msg.setSentDate(new Date());
51
Transport.send(msg);
52
53
System.out.println("Message sent.");
54
}
55
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![None.gif](/Images/OutliningIndicators/None.gif)
7
![None.gif](/Images/OutliningIndicators/None.gif)
8
![None.gif](/Images/OutliningIndicators/None.gif)
9
![None.gif](/Images/OutliningIndicators/None.gif)
10
![None.gif](/Images/OutliningIndicators/None.gif)
11
![None.gif](/Images/OutliningIndicators/None.gif)
12
![None.gif](/Images/OutliningIndicators/None.gif)
13
![None.gif](/Images/OutliningIndicators/None.gif)
14
![None.gif](/Images/OutliningIndicators/None.gif)
15
![None.gif](/Images/OutliningIndicators/None.gif)
16
![None.gif](/Images/OutliningIndicators/None.gif)
17
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
18
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
22
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
23
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
24
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
31
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
32
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
34
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
35
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
36
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
37
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
38
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
39
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
41
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
42
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
43
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
44
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
45
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
46
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
48
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
49
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
50
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
51
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
52
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
53
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
54
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
关于邮件的解析请看 http://www.javayou.com/showlog.jspe?log_id=372
更多关于javamail的文章 http://www.javayou.com/main.jspe?query=javamail