邮箱下发验证码

    

使用javamail发送《任意位数的》位验证码的邮件(完美解决中文乱码问题)

参考:http://blog.csdn.net/kevin_zhao_zl/article/details/67634276 

需要导入jar 包《Java项目》mail.jar 《 javamail1.4.7 

 

如果是maven项目:这需要导入依赖:

 

<dependency>

 

     <groupId>javax.mail</groupId>

 

     <artifactId>mail</artifactId>

 

     <version>1.5.0-b01</version>

 

  </dependency>

            java 项目 :

import java.util.Date;

import java.util.Properties;

import java.util.UUID;

 

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

 

/**

 * FileName: Mail.java

 * @Description:

* All rights Reserved, Designed By ZTE-ITS

 * Copyright: Copyright(C) 2017

 * Company  TWO

 * @author:  YYLI

 * @version    V1.0

 * Createdate:2017年11月2日 下午3:48:54

 */

/**

 * @ClassName: Mail

 * @Description:

 * @author: YYLI

 * @date: 2017年11月2日 下午3:48:54

 */

public class MailUtil {

 

//生成随机验证码

    public static String[] chars = new String[] { "a", "b","c", "d", "e", "f",

        "g", "h", "i", "j","k", "l", "m", "n", "o","p", "q", "r", "s",

        "t", "u", "v", "w","x", "y", "z", "0", "1","2", "3", "4", "5",

        "6", "7", "8", "9","A", "B", "C", "D", "E","F", "G", "H", "I",

        "J", "K", "L", "M","N", "O", "P", "Q", "R","S", "T", "U", "V",

        "W", "X", "Y", "Z" };

   

    //发件人的 邮箱 和 密码(替换为自己的邮箱和密码)

    //PS: 某些邮箱服务器为了增加邮箱本身密码的安全性,给 SMTP 客户端设置了独立密码(有的邮箱称为“授权码”),

    //对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。

    public static String myEmailAccount = "cfpmdzz@163.com";

    public static String myEmailPassword = "cfpmdzz1234";

 

    //发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为:smtp.xxx.com

    //网易163邮箱的 SMTP 服务器地址为: smtp.163.com

    public static String myEmailSMTPHost = "smtp.163.com";

 

    public String SendMail(String receiveMailAccount) throws Exception {

           //1. 创建参数配置, 用于连接邮件服务器的参数配置

           Properties props = new Properties(); // 参数配置

           props.setProperty("mail.transport.protocol","smtp"); // 使用的协议(JavaMail规范要求)

           props.setProperty("mail.smtp.host",myEmailSMTPHost); // 发件人的邮箱的 SMTP

                                                                                                             //服务器地址

           props.setProperty("mail.smtp.auth","true"); // 需要请求认证

          

           //2. 根据配置创建会话对象, 用于和邮件服务器交互

           Session session = Session.getDefaultInstance(props);

           session.setDebug(true);// 设置为debug模式, 可以查看详细的发送 log

 

           //3. 创建一封邮件

           /**

            * 创建一封只包含文本的简单邮件

            *

            * @param session

            *           和服务器交互的会话

            * @param sendMail

            *           发件人邮箱

            * @param receiveMail

            *           收件人邮箱

            * @return

            * @throws Exception

            */

           //  创建一封邮件

           MimeMessage message = new MimeMessage(session);

 

           //  From: 发件人

           message.setFrom(new InternetAddress(myEmailAccount, "中文测试","UTF-8"));

 

           //  To: 收件人(可以增加多个收件人、抄送、密送)

           message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveMailAccount, "亲爱的用户","UTF-8"));

 

           //  Subject: 邮件主题

           message.setSubject("验证码", "UTF-8");

 

           //  Content: 邮件正文(可以使用html标签)

           String RandomCode=generateShortUuid();

          

           String setMessageContent = this.setMessageContent(message,"",RandomCode);

          

           message.setSentDate(new Date());

 

           //  保存设置

           message.saveChanges();

 

           //4. 根据 Session 获取邮件传输对象

           Transport transport = session.getTransport();

 

           //5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错

          

           transport.connect(myEmailAccount,myEmailPassword);

 

           //6. 发送邮件, 发到所有的收件地址,message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人,

           //抄送人, 密送人

           transport.sendMessage(message,message.getAllRecipients());

          

           //8. 关闭连接

           transport.close();

           return setMessageContent;

    }

 

    /*

    @Description:生成八位验证码

    */

    public static String generateShortUuid() {

        StringBuffer shortBuffer = new StringBuffer();

        String uuid =UUID.randomUUID().toString().replace("-", "");

        for (int i = 0; i < 8; i++) {

            String str = uuid.substring(i * 4, i *4 + 4);

            int x = Integer.parseInt(str, 16);

            shortBuffer.append(chars[x %0x3E]);

        }

        return shortBuffer.toString();

    }

    /*

    @解决邮件正文乱码问题

    */

    public String setMessageContent(Message message, String ContentType,String RandomCode)throws MessagingException {

   /** StringContent="尊敬的用户,您好!您的验证码是: "+RandomCode;   这个是八位*/

           String Content="尊敬的用户,您好!您的验证码是: "+(int)((Math.random()*9+1)*1000);  //这个是四位

           MimeMultipart mmt = new MimeMultipart();

           MimeBodyPart mbp = new MimeBodyPart();

 

           if(ContentType == null || ContentType.equals("")) // ContentType为编码类型,如GBK等

               mbp.setText(Content);// 由JavaMail来决定编码

           else

 

           mbp.setContent(Content,ContentType); // 指定编码格式

           mmt.addBodyPart(mbp);

           message.setContent(mmt);

           return Content;

    }

}

 

        测试类:

/**

 * FileName: TestMail.java

 * @Description:

* All rights Reserved, Designed By ZTE-ITS

 * Copyright: Copyright(C) 2017

 * Company  TWO

 * @author:  YYLI

 * @version    V1.0

 * Createdate:2017年11月2日 下午2:50:58

 */

 

 

/**

 * @ClassName: TestMail

 * @Description:

 * @author: YYLI

 * @date: 2017年11月2日 下午2:50:58

 */

public class TestMail {

public static void main(String[] args) {

//收件人信息

String receiveMailAccount = "1510447360@qq.com";

MailUtil mr = new MailUtil();

try {

String sendMail = mr.SendMail(receiveMailAccount);

System.out.println(sendMail);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

 

}

}

 

posted @ 2017-11-02 18:04  小窗幽记  阅读(250)  评论(0编辑  收藏  举报