基于163的邮件发送

来源:互联网 发布:全国乡镇经纬度数据库 编辑:程序博客网 时间:2024/05/18 02:23

我们平常写项目常常会用到邮箱激活功能,因此写一下邮件发送。

public static void sendMail(String to,String code) throws MessagingException {        Properties prop=new Properties();        prop.put("mail.host","smtp.163.com" );        prop.put("mail.transport.protocol", "smtp");        prop.put("mail.smtp.auth", true);        //1.创建sesssion        Session session=Session.getInstance(prop);        //开启session的调试模式,可以查看当前邮件发送状态        session.setDebug(true);        //2.通过session获取Transport对象(发送邮件的核心API)        Transport ts=session.getTransport();        //3.通过邮件用户名密码链接(这是我们163发送邮件的smtp密码,而不是登录密码 注意了)        ts.connect("15918499635@163.com", "qwertyuiop159");        //4.创建邮件        Message msg=createSimpleMail(session,to,code);        //5.发送电子邮件        ts.sendMessage(msg, msg.getAllRecipients());    }    public static MimeMessage createSimpleMail(Session session,String to,String code) throws AddressException,MessagingException{        //创建邮件对象        MimeMessage mm=new MimeMessage(session);        //设置发件人        mm.setFrom(new InternetAddress("15918499635@163.com"));        //设置收件人        mm.setRecipient(Message.RecipientType.TO, new InternetAddress(to));        //设置抄送人        mm.setRecipient(Message.RecipientType.CC, new InternetAddress("15918499635@163.com"));        //主题        mm.setSubject("激活邮件");        // 发送内容        mm.setContent("<h1>请点击连接激活</h1>"                + "         <h3><a href='http://localhost:8080/shop/user_active.action?code="+code+"'>"                        + " http://localhost:8080/shop/user_active.action?code="+code+"</a></h3>",                         "text/html;charset=UTF-8");        return mm;    }    public static void main(String args[]) throws MessagingException {    }
阅读全文
0 0
原创粉丝点击