Gmail目前已經啟用了POP3和SMTP服務,與其他郵箱不同的是Gmail提供的POP3和SMTP是使用安全套接字層SSL的,因此常規的JavaMail程序是無法收發郵件的,下面是使用JavaMail如何收取Gmail郵件以及發送郵件的代碼:
成都創新互聯是一家專注于成都網站建設、成都網站設計與策劃設計,環縣網站建設哪家好?成都創新互聯做網站,專注于網站建設十多年,網設計領域的專業建站公司;建站業務涵蓋:環縣等地區。環縣做網站價格咨詢:18982081108
1.?[代碼]GmailFetch.java?????跳至?[1]?[2]?[全屏預覽]
01????package?lius.javamail.ssl;????
02?????????
03????import?java.io.UnsupportedEncodingException;????
04????import?java.security.*;????
05????import?java.util.Properties;????
06????import?javax.mail.*;????
07????import?javax.mail.internet.InternetAddress;????
08????import?javax.mail.internet.MimeUtility;????
09?????????
10????/**????
11?????*?用于收取Gmail郵件????
12?????*?@author?Winter?Lau????
13?????*/????
14????public?class?GmailFetch?{????
15??????????
16?????public?static?void?main(String?argv[])?throws?Exception?{????
17?????????
18??????Security.addProvider(new?com.sun.net.ssl.internal.ssl.Provider());????
19??????final?String?SSL_FACTORY?=?"javax.net.ssl.SSLSocketFactory";????
20?????????
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?????????
28??????//以下步驟跟一般的JavaMail操作相同????
29??????Session?session?=?Session.getDefaultInstance(props,null);????
30?????????
31??????//請將紅色部分對應替換成你的郵箱帳號和密碼????
32??????URLName?urln?=?new?URLName("pop3","pop.gmail.com",995,null,????
33????????"[郵箱帳號]",?"[郵箱密碼]");????
34??????Store?store?=?session.getStore(urln);????
35??????Folder?inbox?=?null;????
36??????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???????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??????}?finally?{????
58???????try?{????
59????????inbox.close(false);????
60???????}?catch?(Exception?e)?{}????
61???????try?{????
62????????store.close();????
63???????}?catch?(Exception?e)?{}????
64??????}????
65?????}????
66??????????
67?????protected?static?String?decodeText(String?text)????
68???????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?????????
78????}????
2.?[代碼]GmailSender.java?????
01????package?lius.javamail.ssl;????
02?????????
03????import?java.security.Security;????
04????import?java.util.Date;????
05????import?java.util.Properties;????
06?????????
07????import?javax.mail.Authenticator;????
08????import?javax.mail.Message;????
09????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?????????
17????/**????
18?????*?使用Gmail發送郵件????
19?????*?@author?Winter?Lau????
20?????*/????
21????public?class?GmailSender?{????
22?????????
23?????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??????Session?session?=?Session.getDefaultInstance(props,?new?Authenticator(){????
37??????????protected?PasswordAuthentication?getPasswordAuthentication()?{????
38??????????????return?new?PasswordAuthentication(username,?password);????
39??????????}});????
40?????????
41???????????//?--?Create?a?new?message?--????
42??????Message?msg?=?new?MimeMessage(session);????
43?????????
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????}
ListString?too=new?ArrayListString();
too.add("****@163.com");
// too.add("******@163.com");
InternetAddress[]?to_mail?=?new?InternetAddress[too.size()];
for?(int?i?=?0;?i??too.size();?i++)?{?//?設置接收郵件人的地址
to_mail[i]?=?new?InternetAddress(String.valueOf(too.get(i)));
}
messageHelper.setTo(to_mail);
不要用for循環 ?用internetAddress[] ?. 我前幾天也是跟你一樣.
方法:
1.前提準備工作:
首先,郵件的發送方要開啟POP3 和SMTP服務--即發送qq郵件的賬號要開啟POP3 和SMTP服務
2.開啟方法:
登陸qq郵箱
3.點擊 設置
4.點擊—-賬戶
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 —點擊開啟
6.送短信 —–點擊確定
7.稍等一會,很得到一個授權碼! –注意:這個一定要記住,一會用到
8.點擊保存修改 —OK 完成
9.java 測試代碼:
package cn.cupcat.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 連接協議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機名
properties.put("mail.smtp.port", 465);// 端口號
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設置是否顯示debug信息 true 會在控制臺顯示相關信息
//得到回話對象
Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
//設置發件人郵箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最后,祝你也能成功,如果有什么問題,可以一起討論!
注意事項
得到的授權碼一定要保存好,程序中要使用
分享名稱:java代碼接收qq郵件 java發送email
網站鏈接:http://vcdvsql.cn/article4/ddccjie.html
成都網站建設公司_創新互聯,為您提供定制網站、搜索引擎優化、營銷型網站建設、企業建站、面包屑導航、移動網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯