|
|
|
|
|
JAVA Mail 發郵件的例子
|
javamail.jsp 內容如下
<%@page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<html>
<head>
<title>JavaMail 電子郵件發送系統</title>
</head>
<body>
<%
// 以下變量為用戶根據自己的情況設置
String smtphost = "smtp.now.net.cn"; // 發送郵件服務器
String user = "yauboo"; // 郵件服務器登錄用戶名
String password = "xxx"; // 郵件服務器登錄密碼
String from = "yauboo@now.net.cn"; // 發送人郵件地址
String to = "yauboo@now.net.cn"; // 接受人郵件地址
String subject = "Java Mail Subject!"; // 郵件標題
String body = "Java Mail Body!"; // 郵件內容
// 以下為發送程序,用戶無需改動
try {
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
transport.close();
%><p>你的郵件已發送,請返回。</p><%
} catch(Exception m) {
out.println(m.toString());
}%>
</BODY>
</HTML>
|
|
|
|
|
|