标签:uil deb message text 注入 end row sea sage
1、你如果要在maven项目中进行邮箱邮箱验证,你首先要先到pom.xml文件中配置mail.jar,activation.jar包
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>
2、然后你可以写一个简单的jsp页面进行测试
<form action="<c:url value=‘/MailLogin/mail‘/>" method="post"> 邮箱验证<input type="text" name="mail"/><br/><br/> <input type="submit" value="注册"/> </form>
然后在springMVC下面注入@Controll
@Controller @RequestMapping("/MailLogin") public class mailLogin { @RequestMapping("/mail") public String mailSucc(HttpServletRequest request) throws Exception{ String toEMAIL = request.getParameter("mail"); //对方邮箱 String TITLE = "邮箱注册成功ing了"; //标题 String CONTENT =toEMAIL+"你成功激活了!!!"; //内容 JavaEmailSender.sendEmail(toEMAIL, TITLE, CONTENT); return "/mailSucc"; }
之后就写邮件发送代码
package cn.hncu.utils; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.util.MailSSLSocketFactory; public class JavaEmailSender { public static void sendEmail(String toEmailAddress,String emailTitle,String emailContent)throws Exception{ Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.qq.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); /**SSL认证,注意腾讯邮箱是基于SSL加密的,所有需要开启才可以使用**/ MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf); //创建会话 Session session = Session.getInstance(props); //发送的消息,基于观察者模式进行设计的 Message msg = new MimeMessage(session); msg.setSubject(emailTitle); //使用StringBuilder,因为StringBuilder加载速度会比String快,而且线程安全性也不错 StringBuilder builder = new StringBuilder(); builder.append("\n"+emailContent); builder.append("\n时间 " + new Date()); msg.setText(builder.toString()); msg.setFrom(new InternetAddress("你的邮箱号码")); Transport transport = session.getTransport(); transport.connect("smtp.qq.com", "你的邮箱号码", "你的登陆密码、(你开启POP3/SMTP服务申请的独立密码)"); //发送消息 transport.sendMessage(msg, new Address[] { new InternetAddress(toEmailAddress) }); transport.close(); } }
如果你没有设置开启SSL,那么你的程序就会报错,就会出现SMTP服务连接失败,还有就是你的QQ邮箱中的POP3/STMP服务要设置打开,你在测试发送给其他邮箱的时候,你的POP3/STMP服务也要打开,不然就会访问不了,报出连接失败的错误信息来!你要查看你自己的是否发送成功可以自己登陆另一个邮箱去查看,也可以去查看你控制台输出的消息:
/**SSL认证,注意腾讯邮箱是基于SSL加密的,所有需要开启才可以使用**/
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
下面是我发送成功之后控制台输出的消息:
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL true 220 smtp.qq.com Esmtp QQ Mail Server DEBUG SMTP: connected to host "smtp.qq.com", port: 465 EHLO 0R9PX2BMMB5TJIB 250-smtp.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "73400320" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "MAILCOMPRESS", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN succeeded DEBUG SMTP: use8bit false MAIL FROM:<489291805@qq.com> 250 Ok RCPT TO:<job13107175930@sina.com> 250 Ok DEBUG SMTP: Verified Addresses DEBUG SMTP: job13107175930@sina.com DATA 354 End data with <CR><LF>.<CR><LF> From: 489291805@qq.com Message-ID: <549487861.0.1505131325646.JavaMail.Administrator@smtp.qq.com> Subject: =?UTF-8?B?6YKu566x5rOo5YaM5oiQ5YqfaW5n5LqG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
如果你没又没进行设置SSL的话默认的这个端口就是25 ----》
DEBUG SMTP: connected to host "smtp.qq.com", port: 465
标签:uil deb message text 注入 end row sea sage
原文地址:http://www.cnblogs.com/1314wamm/p/7507023.html