當前位置:首頁 » 編程語言 » 發送郵件java

發送郵件java

發布時間: 2025-05-02 22:39:11

1. 如何使用java發送qq郵件

方法:

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("[email protected]"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("[email protected]", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最後,祝你也能成功,如果有什麼問題,可以一起討論!

注意事項

得到的授權碼一定要保存好,程序中要使用

熱點內容
linux的nohup命令 發布:2025-05-03 10:12:03 瀏覽:268
安卓手機同步通訊錄開關在哪裡 發布:2025-05-03 10:04:02 瀏覽:549
QT編譯純c代碼 發布:2025-05-03 10:04:00 瀏覽:170
我的世界伺服器領地熊家 發布:2025-05-03 10:03:53 瀏覽:97
浪潮伺服器怎麼拆硬碟 發布:2025-05-03 09:48:16 瀏覽:104
醉酒爸爸安卓版本在哪裡下載 發布:2025-05-03 09:47:22 瀏覽:54
python線程內存 發布:2025-05-03 09:38:47 瀏覽:467
解壓的玩泥 發布:2025-05-03 09:35:41 瀏覽:197
c語言哈夫曼樹 發布:2025-05-03 09:27:56 瀏覽:91
H的資料庫 發布:2025-05-03 09:21:40 瀏覽:318