?? sendmail.java
字號:
package ch08.section01;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendMail
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=gb2312";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String host = request.getParameter("host");
String from = request.getParameter("from");
String to = request.getParameter("to");
String cc = request.getParameter("cc");
String subject = request.getParameter("subject");
String msg = request.getParameter("msg");
try {
java.util.Properties props = System.getProperties();
props.put("mail.host", host);
Session m_session = Session.getDefaultInstance(props, null);
// 創建message對象
Message m = new MimeMessage(m_session);
InternetAddress[] iAddr = null;
// 設置發送人
m.setFrom(new InternetAddress(from));
//設置接收人
iAddr = InternetAddress.parse(to, false);
m.setRecipients(Message.RecipientType.TO, iAddr);
// 抄送地址
if ( (cc != null) && (cc.trim().length() > 0)) {
iAddr = InternetAddress.parse(cc, false);
m.setRecipients(Message.RecipientType.CC, iAddr);
}
// 設置主題
m.setSubject(subject);
// 設置消息內容
m.setContent(msg, "text/plain");
// 發送時間
m.setSentDate(new Date());
// 發送信息
Transport.send(m);
out.println("發送成功!");
}
catch (Exception ex) {
out.println("發送失敗: " + ex.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
this.doGet(request, response);
}
public void destroy() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -