JavaMail发送邮件

本例需要mail.jar    activation.jar可以到官网下载

表单页:

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'mailinfo.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <%
  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");
  %>
  <body>
    <form action="sendmail2.jsp" method="post">
    <table width="75%" height="75%" border="1" align="center">
    <tr>
    	<td width="50%">
    	接收人: <input name="to" size="25"/>
    	</td>
    	<td width="50%">
    	发送人: <input name="from" size="25"/>
    	</td>
    </tr>
    <tr>
    	<td colspan="2">
    	主题: <input name="subject" size="50"/>
    	</td>
    </tr>
    <tr>
    	<td colspan="2">
    	邮件内容: <br/><textarea rows="15" cols="65" name="text"></textarea>
    	</td>
    </tr>
    <tr>
    	<td colspan="2">
    	<input type="submit" name="cb_submit" value="发送"/>
    	<input type="reset" name="cb_reset" value="清除"/>
    	</td>
    </tr>
    </table>
    </form>
  </body>
</html>

 

 处理表单页即发送邮件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="javax.mail.Session"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.Transport"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'sendmail2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    request.setCharacterEncoding("UTF-8");
    try{
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.qq.com");
    props.put("mail.smtp.auth","true");
    Session ssn = Session.getInstance(props, null);
    MimeMessage message = new MimeMessage(ssn);
    InternetAddress fromAddress = new InternetAddress(request.getParameter("from"));
    message.setFrom(fromAddress);
    InternetAddress toAddress = new InternetAddress(request.getParameter("to"));
    message.addRecipient(Message.RecipientType.TO, toAddress);
    message.setSubject(request.getParameter("subject"));
    message.setText(request.getParameter("text"));
    Transport transport = ssn.getTransport("smtp");
    transport.connect("smtp.qq.com", "[email protected]", "taijunYE123cAMEY");
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
    %>
    <p>邮件发送成功!</p>
    <%
    }catch(Exception e){
    	out.print(e.toString());
    }
    %>
  </body>
</html>
 

你可能感兴趣的:(javamail)