# 如何使用SMTP發郵件?
應用中發送郵件是一個很常見的功能。經過大量用戶實踐反饋,只推薦一種發郵件的方式,即安裝郵件插調用第三方郵件系統的STMP相關賬號來進行郵件發送。
不同于LAMP下發送郵件,Windows下,SMTP發送郵件前,需要以下5個步驟:
1. 下載 [javax.mail.jar](https://download.csdn.net/download/u010182075/7145569)包到 *C:\wwwroot\www.example.com/WEB-INF/lib/* 示例目錄下
>注意:上傳前,如果*C:\wwwroot\www.example.com* 目錄下存在其他文件,請將其清空后再上傳
2. 新建一個以.jsp后綴的文件,假設命名為“index.jsp”,將其放到與 */WEB-INF* 目錄同級上
3. 拷貝下面的配置文件模板到index.jsp文件中,并保存
```
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
String result;
// 收信方Email
String to = "***********@qq.com";
String from = "norelpy@smtp.websoft9.cn";
String psd = "WebSoft23221";
String user = "norelpy@smtp.websoft9.cn";
Properties properties = new Properties();
try {
properties.setProperty( "mail.smtp.auth", "true" );
properties.setProperty( "mail.transport.protocol", "smtp" );
properties.setProperty( "mail.smtp.host", "smtpdm.aliyun.com" );
// SSL
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.socketFactory.port", "465");
properties.setProperty("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance( properties );
Message message = new MimeMessage( mailSession );
Transport transport = mailSession.getTransport();
message.setFrom( new InternetAddress( from ) );
message.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) );
message.setSubject( "This is the Subject Line!" );
BodyPart messageBodyPart = new MimeBodyPart();
//郵件信息內容
messageBodyPart.setText("This is message body");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
message.setSentDate(new Date());
transport.connect(user,psd);
transport.sendMessage( message, message.getAllRecipients() );
result = "Sent message successfully....";
} catch (Exception e) {
e.printStackTrace();
result = "Error: unable to send message....";
}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<%
out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>
```
4. 重啟Tomcat。桌面雙擊Tomcat,單擊`General->stop->start`即可
5. 本地瀏覽器訪問:http://服務器公網IP,
如果網頁上出現 `Send Email using JSP
Result: Sent message successfully.... ` 這樣的反饋信息說明郵件發送成功
## SMTP測試失敗
如果使用第三方提供的SMTP服務(如qq郵箱、網易郵箱等),配置也沒有問題,但是仍然無法發送郵件。請檢查如下兩個問題:
1. win+R,輸入cmd,驗證是否可以連接SMTP,命令如下
~~~
//測試qq郵箱 端口有465和587
telnet smtp.qq.com 465
//測試網易郵箱 端口有465和994
telnet smtp.163.com 465
~~~
如果沒有出現 `正在連接smtp....` 這樣的反饋信息說明可以連接
- 關于本書
- 鏡像安裝包
- 表:鏡像組成一覽
- 附:常用賬號與密碼說明
- Java On Tomcat指南(Linux)
- 如何安裝Java網站?
- 如何設置HTTPS訪問?
- 如何修改上傳的文件權限?
- 如何查看和管理日志文件?
- 如何管理數據庫?
- 常見問題
- 如何部署Java應用
- 如何用SMTP發送郵件
- 如何安裝Redis
- Java On Tomcat指南(Windows)
- 如何安裝Java網站?
- 如何設置HTTPS訪問?
- 如何查看和管理日志文件?
- 如何管理數據庫?
- 常見問題
- 如何用SMTP發送郵件
- 備份
- 升級
- 域名管理
- 域名解析
- 域名綁定
- 云服務器操作
- Linux:登錄與文件管理(SFTP)
- Windows:遠程桌面與文件管理
- 安全組設置
- 快照備份
- 掛載數據盤
- 附:常見Linux命令操作
- 附:服務啟動與停止