2.服务器的原理
3.下载两个jar包
JavaMail API (compat) » 1.4.7
<!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
JavaBeans™ Activation Framework » 1.1.1
<!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>
4.基本类:
5.全部代码
package Com.Jsxs; import com.sun.mail.util.MailSSLSocketFactory; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; //发送一个简单的邮件 public class MailDemo1 { public static void main(String[] args) throws Exception { 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"); prop.put("mail.smtp.ssl.socketFactory",sf); // 使用JavaMail发送邮件的五个步骤 // 1.创建定义整个应用程序所需要的环境信息的session对象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 发件人邮件用户名,授权码 return new PasswordAuthentication("2261203961@qq.com","授权码"); } }); // 开启Session的debug模式,这样就可以看到程序发送Email的运行状态 session.setDebug(true); // 2.通过session得到transport对象 Transport ts = session.getTransport(); // 3.使用邮箱的用户名和授权码连上邮件服务器 ts.connect("smtp.qq.com","2261203961@qq.com","授权码"); // 4.创建邮件: 写邮件 // 注意需要传递Session MimeMessage message = new MimeMessage(session); //指明邮件的发件人 message.setFrom(new InternetAddress("2261203961@qq.com")); //指明邮件的收件人,发件人和收件人一样就是发送给自己 message.setRecipient(Message.RecipientType.TO,new InternetAddress("2261203961@qq.com")); //邮件的标题 message.setSubject("吉士先生"); //邮件的文本内容: message.setContent("<h1 style='color: red'>你好啊,李威涛<h1>","text/html;charset=UTF-8"); // 5.发送邮件 ts.sendMessage(message,message.getAllRecipients()); // 6.关闭邮件链接 ts.close(); } }
(二十七)、网站注册发送邮件实现
package com.Jsxs.Util; import com.Jsxs.projo.User; import com.sun.mail.util.MailSSLSocketFactory; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Properties; // 网站三秒原则 public class Sendmail extends Thread{ // private String from="2261203961@qq.com"; // 邮箱的用户名 private String username="2261203961@qq.com"; // 授权码 private String password="dbiqifywbfxjdjdh"; // 发送邮件的服务器地址 private String host="smtp.qq.com"; private static User user; public Sendmail(User user){ this.user=user; } public void run(){ try { Properties prop = new Properties(); prop.setProperty("mail.host",host); // 设置QQ邮件服务器 prop.setProperty("mail.transport.protocol","smtp"); //邮件发送协议 prop.setProperty("mail.smtp.auth","true"); //需要验证用户密码 // 关于QQ邮箱,还要设置SSL加密,加上一下代码即可 MailSSLSocketFactory sf= null; sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); prop.put("mail.smtp.ssl.enable","true"); prop.put("mail.smtp.ssl.socketFactory",sf); // 使用JavaMail发送邮件的五个步骤 // 1.创建定义整个应用程序所需要的环境信息的session对象 Session session=Session.getDefaultInstance(prop, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 发件人邮件用户名,授权码 return new PasswordAuthentication(username,password); } }); // 开启Session的debug模式,这样就可以看到程序发送Email的运行状态 session.setDebug(true); // 2.通过session得到transport对象 Transport ts = session.getTransport(); // 3.使用邮箱的用户名和授权码连上邮件服务器 ts.connect(host,username,password); // 4.创建邮件: 写邮件 MimeMessage mimeMessage = imageMall(session); // 5.发送邮件 ts.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); // 6.关闭邮件链接 ts.close(); } catch (Exception e) { e.printStackTrace(); } } public static MimeMessage imageMall(Session session) throws MessagingException { // 注意需要传递Session MimeMessage mimeMessage = new MimeMessage(session); //指明邮件的发件人 mimeMessage.setFrom(new InternetAddress("2261203961@qq.com")); //指明邮件的收件人,发件人和收件人一样就是发送给自己 mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail())); //邮件的标题 mimeMessage.setSubject("用户注册"); String info="恭喜您注册成功,您的用户名为:"+user.getUsername()+",您的密码:"+user.getPassword()+",请您妥善管理"; //===================================================================图片// //准备图片数据 MimeBodyPart image = new MimeBodyPart(); //图片需要进行处理 DataHandler dh = new DataHandler(new FileDataSource("E:\\Ideal源码\\emali\\web\\img\\3.png")); image.setDataHandler(dh); //在我们的主题中 放入这个处理后的数据 image.setContentID("haha.jpg"); //给图片设置一个ID //准备正文数据 MimeBodyPart text = new MimeBodyPart(); text.setContent("<img src='cid:haha.jpg'>"+info,"text/html;charset=UTF-8"); //*******************************************************************附件// //附件 MimeBodyPart body3 = new MimeBodyPart(); body3.setDataHandler(new DataHandler(new FileDataSource("E:\\Ideal源码\\mail-java\\src\\Com\\resourece\\log4j.properties"))); body3.setFileName(""); //附件设置名字 MimeBodyPart body4 = new MimeBodyPart(); body4.setDataHandler(new DataHandler(new FileDataSource("E:\\Ideal源码\\mail-java\\src\\Com\\resourece\\1.txt"))); body4.setFileName(""); //附件设置名字 //拼装正文内容---->文本和图片 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("related"); //将拼接好的正文内容设置为主题 MimeBodyPart contentText = new MimeBodyPart(); contentText.setContent(mm); //拼装附件 MimeMultipart allFile = new MimeMultipart(); allFile.addBodyPart(body3); //附件1 allFile.addBodyPart(body4); //附件2 allFile.addBodyPart(contentText); //正文 allFile.setSubType("mixed"); //所有类型都设置为mixed //设置到消息,保存修改 mimeMessage.setContent(allFile); mimeMessage.saveChanges(); //===================================================================// return mimeMessage; } }
package com.Jsxs.Servlet; import com.Jsxs.Util.Sendmail; import com.Jsxs.projo.User; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RegisterServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); System.out.println(username+" "+password+" "+email); User user = new User(); user.setUsername(username); user.setPassword(password); user.setEmail(email); Sendmail sendmail = new Sendmail(user); sendmail.start(); //注册用户; req.setAttribute("message","注册成功,我们已经向您发送了一封电子邮件,请您查收"); req.getRequestDispatcher("info.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
(二十八)、
1.表格提交后转达到Servlet ,那么 只需要写映射的名字即可,不用+/