首页 > 其他 > 详细

代码片段

时间:2018-09-19 23:21:13      阅读:184      评论:0      收藏:0      [点我收藏+]

发送邮件

技术分享图片
package com.boomoom.store.utils;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * 发送邮件的工具类:
 * @author admin
 *
 */
public class MailUtils {

    public static void sendMail(String to,String code){
        
        try {
            // 获得连接:
            Properties props = new Properties();
            Session session = Session.getInstance(props, new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("service@store.com", "111");
                }
                
            });
            // 构建邮件:
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("service@store.com"));
            // 设置收件人:
            // TO:收件人   CC:抄送   BCC:暗送,密送.
            message.addRecipient(RecipientType.TO, new InternetAddress(to));
            // 主题:
            message.setSubject("来自全球最大同性交友社区的激活邮件!");
            // 正文:
            message.setContent("<h1>来自全球最大同性交友社区的激活邮件:请点击下面链接激活!</h1><h3><a href=‘http://localhost:8080/store_v2.0/UserServlet?method=active&code="+code+"‘>http://localhost:8080/store_v2.0/UserServlet?method=active&code="+code+"</a></h3>", "text/html;charset=UTF-8");
        
            // 发送邮件:
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        MailUtils.sendMail("aaa@store.com", "sdfjklsdkljrsiduoigittery");
    }
}
MailUtils

 

代码片段

原文:https://www.cnblogs.com/boomoom/p/9678111.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!