前端
<div style="margin-top: 10px">
<form action="${pageContext.request.contextPath}/register" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
邮箱:<input type="text" name="email"><br/>
<input type="submit" value="注册">
<span></span>
</form>
</div>
Controller
@RequestMapping("/register")
@ResponseBody
public String RegisterServlet(String username,String password,String email) throws Exception {
HashMap<String, String> map = new HashMap<>();
map.put("username",username);
map.put("password",password);
map.put("email",email);
sendEmail(map);
return "注册成功,恭喜您!";
}
public static void sendEmail(Map<String,String> map) throws Exception {
//Properties中 设置属性
Properties prop=new Properties();
prop.setProperty("mail.host","smtp.qq.com");///设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp");///邮件发送协议
prop.setProperty("mail.smtp.auth","true");//需要验证用户密码
//QQ邮箱需要设置SSL加密,原因:大厂。其他邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");//使用安全的连接为true
prop.put("mail.smtp.ssl.socketFactory",sf);//socket工厂,使用自己的socket工厂
//使用javaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需要的环境信息的session对象
//QQ才有!其他邮箱就不用
Session session= Session.getDefaultInstance(prop, new Authenticator() {//获取默认的实例
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱、授权码
return new PasswordAuthentication(map.get("email"),"lzycenhxoqujgcje");
}
});
//开启session的debug模式,这样可以查看到程序发送Email的运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport ts = session.getTransport();
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect("smtp.qq.com",map.get("email"),"lzycenhxoqujgcje");
//4.创建邮件:写邮件
//需要传递session
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress(map.get("email")));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress(map.get("email")));
//邮件的标题 只包含文本的简单邮件
message.setSubject("注册通知");
String info="恭喜你("+map.get("username")+")用户注册成功!这是你的密码,请查收"+map.get("password");
//邮件的文本内容
message.setContent(info,"text/html;charset=UTF-8");
message.saveChanges();
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());
//6.关闭连接,一切网络都需要关闭
ts.close();
}
上面是传纯文本的,想传附件和图片,看下面
//带图片和附件的邮件
public class MailDemo01 {
public static void main(String[] args) throws Exception {
//Properties中 设置属性
Properties prop=new Properties();
prop.setProperty("mail.host","smtp.qq.com");///设置QQ邮件服务器
prop.setProperty("mail.transport.protocol","smtp");///邮件发送协议
prop.setProperty("mail.smtp.auth","true");//需要验证用户密码
//QQ邮箱需要设置SSL加密,原因:大厂。其他邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");//使用安全的连接为true
prop.put("mail.smtp.ssl.socketFactory",sf);//socket工厂,使用自己的socket工厂
//使用javaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需要的环境信息的session对象
//QQ才有!其他邮箱就不用
Session session= Session.getDefaultInstance(prop, new Authenticator() {//获取默认的实例
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱、授权码
return new PasswordAuthentication("*@qq.com","**");
}
});
//开启session的debug模式,这样可以查看到程序发送Email的运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport ts = session.getTransport();
//3.使用邮箱的用户名和授权码连上邮件服务器
ts.connect("smtp.qq.com","*@qq.com","**");
//4.创建邮件:写邮件
//需要传递session
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("*@qq.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("*@qq.com"));
//邮件的标题 只包含文本的简单邮件
message.setSubject("发送的标题");
//邮件的文本内容
message.setContent("你好","text/html;charset=UTF-8");
/*==================图片和附件的邮件=========================*/
//=================================准备图片数据
MimeBodyPart image = new MimeBodyPart();
//图片需要经过数据化的处理 DataHandler:数据处理 FileDataSource:加载文件的资源
DataHandler dh = new DataHandler(new FileDataSource("D:\\1.png"));
//在part中放入这个处理的图片数据
image.setDataHandler(dh);
//给这个part设置一个ID名字
image.setContentID("bz.jpg");
//=================================准备正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一张正文<img src='cid:bz.jpg'>","text/html;charset=UTF-8");
//=================================准备附件数据
MimeBodyPart body1= new MimeBodyPart();
body1.setDataHandler(new DataHandler(new FileDataSource("D:\\5.txt")));
body1.setFileName("1.txt");
//描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(body1);
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("mixed");
//设置到消息中,保存修改
message.setContent(mm);
message.saveChanges();
/*===========================================*/
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());
//6.关闭连接,一切网络都需要关闭
ts.close();
}
}