Java mail

login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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>Happy邮件系统</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>
		<center><h2>Happy邮件系统-登录</h2></center>
	    <form name="form1" method="post" action="handle_login.jsp">
			<center>
				<h3><font color="red">登录邮箱</font><br></h3>
				<table>
					<tr>
						<td>邮箱地址:</td><td><input name="emailAddress" type="text" value="756355817@qq.com"></td></tr>
					<tr>
						<td>邮箱密码:</td><td><input name="emailPassword" type="password"></td>
					</tr>
					<tr>
						<td colspan="2">
							<center><input type="submit" name="Submit" value="登录"></center>
						</td>
					</tr>
				</table>
			</center>
		</form>
	</body>
</html>
send_mail.jsp
<%@page import="javax.mail.Transport"%>
<%@page import="javax.activation.DataHandler"%>
<%@page import="javax.activation.FileDataSource"%>
<%@page import="javax.mail.BodyPart"%>
<%@page import="javax.mail.internet.MimeMultipart"%>
<%@page import="javax.mail.internet.MimeUtility"%>
<%@page import="javax.mail.internet.MimeBodyPart"%>
<%@page import="javax.mail.Multipart"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Session"%>


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <%!
 	//解决请求参数中文乱码
 	public String code(String str){
 		String backStr = str;
 		try{
 			backStr = new String(str.getBytes("ISO-8859-1"));
 		}catch(Exception e){e.printStackTrace();}
 		return backStr;
 	}
 	//从文件路径中获取文件名
 	public String getFileName(String path){
 		String fileName = null;
 		int i = path.lastIndexOf("\\");
 		fileName = path.substring(i+1);
 		return fileName;
 	}
 %>
 </head>
 <body>
 <%
 	String to=code(request.getParameter("to"));
 	String title=code(request.getParameter("title"));
 	String content=code(request.getParameter("content"));
 	String attachment =code(request.getParameter("attachmenth"));
 	
 	//获取刚登陆成功的Email地址和密码
 	String emailAddress=(String)session.getAttribute("emailAddress");
	String emailPassword=	(String)session.getAttribute("emailPassword");
	
	Properties prop =new Properties();
	prop.put("mail.smtp.host","smtp.qq.com");
	prop.put("mail.smtp.auth",true);
	Session mail_session=Session.getInstance(prop);
	
	try
	{
	  //创建邮件对象
	  MimeMessage msg =new MimeMessage(mail_session);
	  msg.setFrom(new InternetAddress(emailAddress));
	  msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
	  msg.setSubject(title);
	  //msg.setText(content);
	  //创建邮件内容部分,邮件不止一个部分
	  MimeMultipart multipart = new MimeMultipart();
	  
	  //往内容中添加正文部分
	  MimeBodyPart bodypart = new MimeBodyPart();
	  bodypart.setContent(content,"text/html;charset=gb2312");
	  multipart.addBodyPart(bodypart);
	  
	  //DataHandler dh =new DataHandler("JavaMail附件测试","text/plain;cherset=gb232");
	  //FileDataSource fds =new FileDataSource("c:/story.txt");
	  //dh =new DataHandler(fds);
	  //bodypart.setFileName("story.txt");//可以和原文件名不一致
	  //bodypart.setDataHandler(dh);
	  //multipart.addBodyPart( bodypart);
	  
	  
	  
	  
	  
	  msg.setContent(multipart);
	  
	  Transport ts = mail_session.getTransport("smtp");
	  ts.connect("smtp.qq.com",emailAddress,emailPassword);
	  
	  ts.sendMessage(msg,msg.getAllRecipients());
	  ts.close();
	  
	  //发送成功
	  out.println("send message success!");
	
	}
	catch(Exception ex)
	{
	  //发送失败
	  out.println("send message fail!");
	  ex.printStackTrace();
	}
	
 %>
 </body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值