最近項(xiàng)目在做新聞爬蟲(chóng),想實(shí)現(xiàn)這個(gè)功能:爬蟲(chóng)某個(gè)頁(yè)面失敗后,把這個(gè)頁(yè)面的 url 發(fā)到郵箱。最終實(shí)現(xiàn)的效果圖如下,后期可以加上過(guò)濾標(biāo)簽、失敗狀態(tài)碼等,方便分類(lèi)搜索異常。
開(kāi)發(fā)人員可以根據(jù)郵件里的 url 和堆棧信息,分析爬蟲(chóng)失敗的原因。
- 是不是服務(wù)器 down 了?
- 還是爬蟲(chóng)的 Dom 解析沒(méi)有解析到內(nèi)容?
- 還是正則表達(dá)式對(duì)于這個(gè)頁(yè)面不適用?
開(kāi)啟SMTP服務(wù)
在 QQ 郵箱里的 設(shè)置->賬戶(hù)里開(kāi)啟 SMTP 服務(wù)
注意開(kāi)啟完之后,QQ 郵箱會(huì)生成一個(gè)授權(quán)碼,在代碼里連接郵箱使用這個(gè)授權(quán)碼而不是原始的郵箱密碼,這樣可以避免使用明文密碼。
網(wǎng)上查了一下例子,根據(jù)這篇文章 Java Mail(二):JavaMail介紹及發(fā)送一封簡(jiǎn)單郵件 的示例代碼。
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
|
Properties props = new Properties(); // 開(kāi)啟debug調(diào)試 props.setProperty( "mail.debug" , "true" ); // 發(fā)送服務(wù)器需要身份驗(yàn)證 props.setProperty( "mail.smtp.auth" , "true" ); // 設(shè)置郵件服務(wù)器主機(jī)名 props.setProperty( "mail.host" , "smtp.qq.com" ); // 發(fā)送郵件協(xié)議名稱(chēng) props.setProperty( "mail.transport.protocol" , "smtp" ); Session session = Session.getInstance(props); //郵件內(nèi)容部分 Message msg = new MimeMessage(session); msg.setSubject( "seenews 錯(cuò)誤" ); StringBuilder builder = new StringBuilder(); builder.append( "url = " + "http://blog.csdn.net/never_cxb/article/details/50524571" ); builder.append( "頁(yè)面爬蟲(chóng)錯(cuò)誤" ); builder.append( "\n data " + TimeTool.getCurrentTime()); msg.setText(builder.toString()); //郵件發(fā)送者 msg.setFrom( new InternetAddress( "**發(fā)送人的郵箱地址**" )); //發(fā)送郵件 Transport transport = session.getTransport(); transport.connect( "smtp.qq.com" , "**發(fā)送人的郵箱地址**" , "**你的郵箱密碼或者授權(quán)碼**" ); transport.sendMessage(msg, new Address[] { new InternetAddress( "**接收人的郵箱地址**" ) }); transport.close(); |
但是報(bào)錯(cuò)了
1
2
3
4
|
DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN failed Exception in thread "main" javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl). More information at http: //service.mail.qq.com/cgi-bin/help?id=28 |
因?yàn)槭纠a是用的163郵箱,而筆者是 QQ 郵箱,看 Log 分析是 QQ 郵箱需要 SSL 加密。
開(kāi)啟 SSL 加密
網(wǎng)上搜了一下,其他比如163、新浪郵箱不需要 SSL 加密,可以放棄 QQ 郵箱。
網(wǎng)上還有種說(shuō)法,把 smtp.qq.com 換成 smtp.exmail.qq.com也不需要 SSL加密,但是筆者沒(méi)有run成功。所以還是老老實(shí)實(shí)加上 SSL 加密吧。
下面的代碼開(kāi)啟了 SSL 加密
1
2
3
4
|
MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts( true ); props.put( "mail.smtp.ssl.enable" , "true" ); props.put( "mail.smtp.ssl.socketFactory" , sf); |
成功了,控制臺(tái)輸出 Log 和效果圖如下
1
2
3
4
5
6
7
8
9
10
|
DEBUG SMTP: useEhlo true , useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com" , port 465 , isSSL true 220 smtp.qq.com Esmtp QQ Mail Server DEBUG SMTP: connected to host "smtp.qq.com" , port: 465 ... data 2016 - 01 - 19 17 : 00 : 44 Tue . 250 Ok: queued as QUIT 221 Bye |
完整代碼示例
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
|
public class MailTool { public static void main(String[] args) throws MessagingException, GeneralSecurityException { Properties props = new Properties(); // 開(kāi)啟debug調(diào)試 props.setProperty( "mail.debug" , "true" ); // 發(fā)送服務(wù)器需要身份驗(yàn)證 props.setProperty( "mail.smtp.auth" , "true" ); // 設(shè)置郵件服務(wù)器主機(jī)名 props.setProperty( "mail.host" , "smtp.qq.com" ); // 發(fā)送郵件協(xié)議名稱(chēng) props.setProperty( "mail.transport.protocol" , "smtp" ); MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts( true ); props.put( "mail.smtp.ssl.enable" , "true" ); props.put( "mail.smtp.ssl.socketFactory" , sf); Session session = Session.getInstance(props); Message msg = new MimeMessage(session); msg.setSubject( "seenews 錯(cuò)誤" ); StringBuilder builder = new StringBuilder(); builder.append( "url = " + "http://blog.csdn.net/never_cxb/article/details/50524571" ); builder.append( "\n頁(yè)面爬蟲(chóng)錯(cuò)誤" ); builder.append( "\n時(shí)間 " + TimeTool.getCurrentTime()); msg.setText(builder.toString()); msg.setFrom( new InternetAddress( "**發(fā)送人的郵箱地址**" )); Transport transport = session.getTransport(); transport.connect( "smtp.qq.com" , "**發(fā)送人的郵箱地址**" , "**你的郵箱密碼或者授權(quán)碼**" ); transport.sendMessage(msg, new Address[] { new InternetAddress( "**接收人的郵箱地址**" ) }); transport.close(); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。