最近項目在做新聞爬蟲,想實現這個功能:爬蟲某個頁面失敗后,把這個頁面的 url 發到郵箱。最終實現的效果圖如下,后期可以加上過濾標簽、失敗狀態碼等,方便分類搜索異常。
開發人員可以根據郵件里的 url 和堆棧信息,分析爬蟲失敗的原因。
- 是不是服務器 down 了?
- 還是爬蟲的 Dom 解析沒有解析到內容?
- 還是正則表達式對于這個頁面不適用?
開啟SMTP服務
在 QQ 郵箱里的 設置->賬戶里開啟 SMTP 服務
注意開啟完之后,QQ 郵箱會生成一個授權碼,在代碼里連接郵箱使用這個授權碼而不是原始的郵箱密碼,這樣可以避免使用明文密碼。
網上查了一下例子,根據這篇文章 Java Mail(二):JavaMail介紹及發送一封簡單郵件 的示例代碼。
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(); // 開啟debug調試 props.setProperty( "mail.debug" , "true" ); // 發送服務器需要身份驗證 props.setProperty( "mail.smtp.auth" , "true" ); // 設置郵件服務器主機名 props.setProperty( "mail.host" , "smtp.qq.com" ); // 發送郵件協議名稱 props.setProperty( "mail.transport.protocol" , "smtp" ); Session session = Session.getInstance(props); //郵件內容部分 Message msg = new MimeMessage(session); msg.setSubject( "seenews 錯誤" ); StringBuilder builder = new StringBuilder(); builder.append( "url = " + "http://blog.csdn.net/never_cxb/article/details/50524571" ); builder.append( "頁面爬蟲錯誤" ); builder.append( "\n data " + TimeTool.getCurrentTime()); msg.setText(builder.toString()); //郵件發送者 msg.setFrom( new InternetAddress( "**發送人的郵箱地址**" )); //發送郵件 Transport transport = session.getTransport(); transport.connect( "smtp.qq.com" , "**發送人的郵箱地址**" , "**你的郵箱密碼或者授權碼**" ); transport.sendMessage(msg, new Address[] { new InternetAddress( "**接收人的郵箱地址**" ) }); transport.close(); |
但是報錯了
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 |
因為示例代碼是用的163郵箱,而筆者是 QQ 郵箱,看 Log 分析是 QQ 郵箱需要 SSL 加密。
開啟 SSL 加密
網上搜了一下,其他比如163、新浪郵箱不需要 SSL 加密,可以放棄 QQ 郵箱。
網上還有種說法,把 smtp.qq.com 換成 smtp.exmail.qq.com也不需要 SSL加密,但是筆者沒有run成功。所以還是老老實實加上 SSL 加密吧。
下面的代碼開啟了 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); |
成功了,控制臺輸出 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(); // 開啟debug調試 props.setProperty( "mail.debug" , "true" ); // 發送服務器需要身份驗證 props.setProperty( "mail.smtp.auth" , "true" ); // 設置郵件服務器主機名 props.setProperty( "mail.host" , "smtp.qq.com" ); // 發送郵件協議名稱 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 錯誤" ); StringBuilder builder = new StringBuilder(); builder.append( "url = " + "http://blog.csdn.net/never_cxb/article/details/50524571" ); builder.append( "\n頁面爬蟲錯誤" ); builder.append( "\n時間 " + TimeTool.getCurrentTime()); msg.setText(builder.toString()); msg.setFrom( new InternetAddress( "**發送人的郵箱地址**" )); Transport transport = session.getTransport(); transport.connect( "smtp.qq.com" , "**發送人的郵箱地址**" , "**你的郵箱密碼或者授權碼**" ); transport.sendMessage(msg, new Address[] { new InternetAddress( "**接收人的郵箱地址**" ) }); transport.close(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助。