JSP中的郵件收發(1)

來源:互聯網
上載者:User

標籤:java   os   io   檔案   ar   cti   時間   new   

JavaMail API是發送和接收電子郵件資訊的可選包,將下載到的壓縮包解壓,將解壓目錄中的mail.jar和lib檔案夾下面的所有jar類包都拷貝到網站類路徑中,此外,還需下載JavaBeans Activation Framework,因為這個架構是JavaMail API鎖需要的,將解壓目錄下的activation.jar檔案拷貝到網站類路徑中

一.郵件發送的實現

1.從靜態頁面中獲得資訊

String address = request.xxx;

String subject = request.xxx;

String content = request.xxx;

2.建立郵件會話

Properties props = new Properties();

3.儲存發送郵件伺服器的資訊,如126郵箱

props.put("mail.smtp.host","smtp.126.com");

4.同時通過驗證

props.put("mail.smtp.auth","true");

5.根據屬性建立一個郵件會話

Session s = Session.getInstance(props);

s.setDebug(true);

6.由郵件會話建立一個訊息對象

MimeMessage message = new MimeMessage(s);

7.設定郵件

InternetAddress from  = new InternerAddress("[email protected]");

8.設定寄件者

message.setFrom(from);

InternetAddress to = new InternetAddress(address);

9.設定收件者

message.setRecipient(Message.RecipientType.TO,to);

10設定標題,建立內容,發信時間

.......

11.儲存郵件資訊

message.save();

12.建立運輸類

Transport transport = s.getTransport("smtp");

13.以smtp方式登入郵箱,第一個參數是發送郵件用的郵件伺服器SMTP地址,第二個參數為使用者名稱,第三個參數為密碼

transport.connect("smtp.126.com","username","password");

14.發送郵件,第二個參數是所有已設好的收件者地址

transport.sendMessage(message,message.getAllRecipients());

15.關閉

trasport.close();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.