在Maven項目中關於SSM框架中郵箱驗證登陸


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
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM