1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package whut.mailsender; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import whut.mailreceiver.MailAuthenticator; public class SimpleSMTPSender public static void main(String[] try { Properties new Properties(); //传递一个邮件服务器名smtp.163.com //mail.smtp.host代表是发信人所在的邮箱服务器名 props.put( "mail.smtp.host" , "smtp.163.com" ); props.put( "mail.smtp.auth" , true ); //对于发送邮件,只需要保证发送人所在的邮件服务器正确打开就可以了 //收信人的邮箱可以是任意地址,如@163.com,@qq.com,@126.com //创建一个程序与邮件服务器的通信 Session null ); Message new MimeMessage(mailConnection); //创建一个要输入用户名和指令的 //Session //设置发送人和接受人 Address new InternetAddress( "yyyy@163.com" ); Address new InternetAddress( "xxx@163.com" ); /* * * * * * * * */ msg.setFrom(sender); msg.setRecipient(Message.RecipientType.TO, msg.setSubject( "You ); //msg.setContent("Hello", //下面是模拟发送带附件的邮件 //新建一个MimeMultipart对象用来存放多个BodyPart对象 Multipart new MimeMultipart(); //------设置信件文本内容------ //新建一个存放信件内容的BodyPart对象 BodyPart new MimeBodyPart(); //给BodyPart对象设置内容和格式/编码方式 mdp.setContent( "hello" , "text/html;charset=gb2312" ); //将含有信件内容的BodyPart加入到MimeMultipart对象中 mtp.addBodyPart(mdp); //设置信件的附件(用本地机上的文件作为附件) mdp= new MimeBodyPart(); FileDataSource new FileDataSource( "f:/webservice.doc" ); DataHandler new DataHandler(fds); mdp.setFileName( "webservice.doc" ); //可以和原文件名不一致 mdp.setDataHandler(dh); mtp.addBodyPart(mdp); //把mtp作为消息对象的内容 msg.setContent(mtp); //以上为发送带附件的方式 //先进行存储邮件 msg.saveChanges(); Transport "smtp" ); String "yyyy@163.com" ; String "" ; //邮件服务器名,用户名,密码 trans.connect( "smtp.163.com" , trans.sendMessage(msg, trans.close(); } catch (Exception { System.err.println(e); } finally { System.exit( 0 ); } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package whut.mailreceiver; import java.io.InputStreamReader; import java.io.Reader; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; import java.util.Properties; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Header; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeUtility; //利用POP3来读取邮件 //主要用来检测消息Message的基本信息,如发送者,收信者,时间 public class POP3Client public static void main(String[] Properties String "pop3.163.com" ; String "22222222@163.com" ; String "312313121" ; String "pop3" ; try { // Session null ); // Store // store.connect(host, // // Folder "INBOX" ); if (inbox null ) System.out.println( "NO ); System.exit( 1 ); } // inbox.open(Folder.READ_ONLY); System.out.println( "TOTAL + // Message[] for ( int i 0 ; System.out.println( "------------Message--" + 1 ) + "------------" ); // String if (from null ) String System.out.println( "From:" + } String .getReplyTo()); if (replyTo null ) { String System.out.println( "Reply + } String .getRecipients(Message.RecipientType.TO)); if (to null ) String System.out.println( "To:" + } String if (subject null ) System.out.println( "Subject:" + SimpleDateFormat new SimpleDateFormat( "yyyy-MM-dd ); Date if (sent null ) System.out.println( "Sent + Date if (ress null ) System.out.println( "Receive + // // // // // // // // // // // // // // // // // // // // // // // // // // // } // // inbox.close( false ); store.close(); } catch (Exception System.err.println(e); } } // public static String String try { if (from.startsWith( "=?GB" ) "=?gb" ) || "=?UTF" )) from } else { from new String(from.getBytes( "ISO8859_1" ), "GBK" ); } } catch (Exception e.printStackTrace(); } return from; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package whut.mailreceiver; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; //口令验证 public class MailAuthenticator extends Authenticator private JDialog new JDialog( new JFrame(), true ); private JLabel new JLabel( "Please ); private JLabel new JLabel( "User ); private JLabel new JLabel( "Password:" ); private JTextField new JTextField( 20 ); private JPasswordField new JPasswordField( 20 ); private JButton new JButton( "OK" ); public MailAuthenticator() { this ( "" ); } public MailAuthenticator(String { Container pane.setLayout( new GridLayout( 4 , 1 )); pane.add(mainLabel); JPanel new JPanel(); p2.add(userLabel); p2.add(usernameField); pane.add(p2); JPanel new JPanel(); p3.add(passwordLabel); p3.add(passwordField); pane.add(p3); JPanel new JPanel(); p4.add(okButton); pane.add(p4); passwordDialog.setSize( 400 , 200 ); ActionListener new HideDialog(); okButton.addActionListener(al); usernameField.addActionListener(al); passwordField.addActionListener(al); } class HideDialog implements ActionListener { public void actionPerformed(ActionEvent { passwordDialog.hide(); } } @Override protected PasswordAuthentication // passwordDialog.show(); //getPassword是返回的字符数组 String String new String(passwordField.getPassword()); passwordField.setText( "" ); return new PasswordAuthentication(username,password); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package whut.mailsender; import javax.mail.Part; import javax.mail.internet.MimeUtility; import sun.misc.BASE64Decoder; public class StringUtil //发信人,收信人,回执人邮件中有中文处理乱码,res为获取的地址 //http默认的编码方式为ISO8859_1 //对含有中文的发送地址,使用MimeUtility.decodeTex方法 //对其他则把地址从ISO8859_1编码转换成gbk编码 public static String String try { if (from.startsWith( "=?GB" ) "=?gb" ) || "=?UTF" )) from } else { from new String(from.getBytes( "ISO8859_1" ), "GBK" ); } } catch (Exception e.printStackTrace(); } return from; } //转换为GBK编码 public static String try { if (strvalue null ) return null ; else { strvalue new String(strvalue.getBytes( "ISO8859_1" ), "GBK" ); return strvalue; } } catch (Exception return null ; } } //接收邮件时,获取某个邮件的中文附件名,出现乱码 //对于用base64编码过的中文,则采用base64解码, //否则对附件名进行ISO8859_1到gbk的编码转换 public static String throws Exception String // if ((temp.startsWith( "=?GBK?" ) "?=" )) || "=?gbk?b?" ) "?=" ))) temp 8 , "?=" ) 1 )); } else { temp } return temp; } public static String if (s null ) return null ; BASE64Decoder new BASE64Decoder(); try { byte [] return new String(b); } catch (Exception return null ; } } }
文章来源: http://computerdragon.blog.51cto.com/6235984/1197390
|