採用標準 MVC 分層:前端 JSP 表單(message.jsp)收集使用者資料 → 後端處理頁(message_do.jsp)驗證參數 → 呼叫 JavaMail API 透過 SMTP 發送郵件 → 回饋結果。
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head><title>線上留言/聯絡表單</title></head>
<body>
<form action="message_do.jsp" method="post">
<table align="center" border="0" cellpadding="5" cellspacing="0">
<tr><td>信件標題:<font color="red">*</font></td>
<td><input type="text" name="topic" id="topic" class="input"></td></tr>
<tr><td valign="top">反饋信息:<font color="red">*</font></td>
<td><textarea name="body" id="body" rows="5" cols="50" class="input"></textarea></td></tr>
<tr><td>稱呼:<font color="red">*</font></td>
<td><input type="text" name="name" id="name" class="input"></td></tr>
<tr><td>您的郵件:<font color="red">*</font></td>
<td><input type="text" name="email" id="email" class="input"></td></tr>
<tr><td>電話:</td>
<td><input type="text" name="phone" id="phone" class="input"></td></tr>
<tr><td>公司/網址:</td>
<td><input type="text" name="http" id="http" size="40" class="input"></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="提交" name="tt" class="bt" onclick="return check()">
</td></tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*,java.util.*,javax.mail.*,javax.mail.internet.*,javax.activation.*" %>
<%@ page import="cn.cmpy.company.*" %>
<html>
<head><title>留言發送處理</title></head>
<body>
<%
// 1. 取得收件人信箱(此處從資料庫讀取公司設定)
jsp:useBean id="company" scope="page" class="cn.cmpy.company.company"/>
jsp:useBean id="comCtl" scope="page" class="cn.cmpy.company.comCtl"/>
company = comCtl.selectCompany(1);
String toEmail = (company.getEmail() != null) ? company.getEmail() : "nbybc@sina.com";
// 2. 取得表單參數
String username = request.getParameter("name");
String topic = request.getParameter("topic");
String body = request.getParameter("body");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String http = request.getParameter("http");
// 3. 組裝郵件內容
String mailBody = "標題:" + topic + "\n內容:" + body +
"\n聯系人:" + username + "\nE-mail:" + email +
"\n電話:" + phone + "\n公司/網址:" + http;
// 4. JavaMail 發送流程
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.sohu.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("sonic260@sohu.com")); // 寄件者
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
msg.setSubject("網上留言信息");
msg.setSentDate(new java.util.Date());
msg.setText(mailBody);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.sohu.com", "sonic260", "123456789"); // 帳號密碼
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
out.print("");
} catch (Exception e) {
out.println("發送失敗:" + e.toString());
e.printStackTrace();
}
%>
</body>
</html>
WEB-INF/lib 放入 mail.jar、activation.jar(Java 6 以上內建 activation)。context.xml 或屬性檔,避免硬編碼。| 現象 | 可能原因 | 解決方向 |
|---|---|---|
| 535 Authentication failed | 帳號/密碼錯誤、未開啟 SMTP 認證 | 確認郵件服務商設定、使用應用程式專用密碼 |
| Connection timeout | 防火牆阻擋 25/465/587 埠、主機名稱錯誤 | telnet 測試連通性、檢查 DNS 解析 |
| 中文亂碼 | 未設定郵件編碼 | msg.setText(body, "UTF-8") 或使用 MimeBodyPart 設定 charset |