%@ page import="java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%>
赞
踩
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ page import="java.util.*,javax.mail.*"%>
- <%@ page import="javax.mail.internet.*,javax.activation.*"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
-
- </head>
- <body>
- <%
- // 以下变量为用户根据自己的情况设置
- String smtphost = "smtp.qq.com"; // 发送邮件服务器
- String user = "xxxxxxxx@qq.com"; // 邮件服务器登录用户名
- String password = "xxxxxx"; // 邮件服务器登录密码
- String from = "xxxxxxx@qq.com"; // 发送人邮件地址
- String to = "xxxxxxxxx@163.com"; // 接受人邮件地址
- String subject = "JavaMail 电子邮件发送系统测试"; // 邮件标题
- String body = "JavaMail 电子邮件发送系统"; // 邮件内容
-
-
- // 以下为发送程序,用户无需改动
- try {
- //初始化Properties类对象
- Properties props = new Properties();
- //设置mail.smtp.host属性
- props.put("mail.smtp.host", smtphost);
- //设置使用验证
- props.put("mail.smtp.auth","true");
- // 获取非共享的session对象
- Session ssn= Session.getInstance(props,null);
- //创建一个默认的MimeMessage对象。
- MimeMessage message = new MimeMessage(ssn);
- //创建InternetAddress对象
- InternetAddress fromAddress = new InternetAddress(from);
- //设置From: 头部的header字段
- message.setFrom(fromAddress);
- //创建InternetAddress对象
- InternetAddress toAddress = new InternetAddress(to);
- //设置 To: 头部的header字段
- message.addRecipient(Message.RecipientType.TO, toAddress);
- //设置 Subject: header字段
- 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());
- m.printStackTrace();
- }
- %>
- </body>
- </html>
其实区别只在于
message.setContent(body, "text/html;charset=utf8");这一行
- //打印报表数据
- for(Map<String,Object>sc : RecordData){
- System.out.println(sc.get("date"));
- System.out.println(sc.get("amount"));
- System.out.println(sc.get("object"));
-
- cont +="<tr>";
- cont+="<td>"+sc.get("date")+"</td>" ;
- cont+="<td>"+sc.get("amount")+"</td>" ;
- cont+="<td>"+sc.get("object")+"</td>" ;
- cont +="</tr>";
- }
-
-
-
- }
-
- //发送邮件
-
- // 以下变量为用户根据自己的情况设置
- String smtphost = "smtp.qq.com"; // 发送邮件服务器
- String user = "xxxx@qq.com"; // 邮件服务器登录用户名
- String password = "xxxxxxx"; // 邮件服务器登录密码
- String from = "xxxxx@qq.com"; // 发送人邮件地址
- String to = "xxxxx@163.com"; // 接受人邮件地址
- String subject = "webATM账单"; // 邮件标题
- String body = "<html><head><meta charset='utf-8'></head><body><h1>账单</h1><table border='1'><tr><td>交易时间</td><td>交易金额</td><td>交易类型</td></tr>"
- +cont
- +"</table>"
- + "</body></html>"; // 邮件内容
-
- // 以下为发送程序,用户无需改动
- try {
- //初始化Properties类对象
- Properties props = new Properties();
- //设置mail.smtp.host属性
- props.put("mail.smtp.host", smtphost);
- //设置使用验证
- props.put("mail.smtp.auth","true");
- // 获取非共享的session对象
- Session ssn= Session.getInstance(props,null);
- //创建一个默认的MimeMessage对象。
- MimeMessage message = new MimeMessage(ssn);
- //创建InternetAddress对象
- InternetAddress fromAddress = new InternetAddress(from);
- //设置From: 头部的header字段
- message.setFrom(fromAddress);
- //创建InternetAddress对象
- InternetAddress toAddress = new InternetAddress(to);
- //设置 To: 头部的header字段
- message.addRecipient(Message.RecipientType.TO, toAddress);
- //设置 Subject: header字段
- message.setSubject(subject);
- // 现在设置的实际消息
- message.setContent(body, "text/html;charset=utf8");
- //定义发送协议
- Transport transport = ssn.getTransport("smtp");
- //建立与服务器的链接
- transport.connect(smtphost, user, password);
- //发送邮件
- transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
- //transport.send(message);
- //关闭邮件传输
- transport.close();
-
- } catch(Exception m) //捕获异常
- {
- System.out.println(m.toString());
- m.printStackTrace();
- }
-
-
- response.sendRedirect("success.jsp");
-
-
-
-
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。