黑马程序员技术交流社区

标题: 【西安校区】JavaMail发送网络邮件 [打印本页]

作者: 西安Java组    时间: 2018-3-1 21:35
标题: 【西安校区】JavaMail发送网络邮件
本帖最后由 西安Java组 于 2018-3-1 21:35 编辑

背景介绍:

在大多数管理系统中都会有发送邮件的功能,邮件是管理系统与用户之间保持沟通的一种可靠的方式,目前常见的邮箱有有QQ和网易等,那么本文就针对这两类邮箱分别提供可直接使用的工具类.

代码实现   


发送网易邮件的Java类   
[Java] 纯文本查看 复制代码
import java.security.GeneralSecurityException;
import java.util.Properties;

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

public class MailUtils_163 {

        private static String userName = "发件人邮箱账号";//发 送 邮 件的1 6 3邮 箱 账 号  
    private static String password = "发件人邮箱密码";//1 6 3 邮 箱 的 授 权 码,若 没 有 则 使 用 密 码  
    private static String userName2 = "收件人邮箱账号";//接 收 邮 件 的 1 6 3 邮 箱 账 号  
    private static String port = "25";//端 口 号  
    /**
     * 该 方 法 用 来 发 送 邮 件
     * @param to:给 谁 发 邮 件
     * **/  
    public static void sendMain(String to) throws AddressException, MessagingException, GeneralSecurityException{  
        //1、创 建 连 接 对 象,连 接 到 邮 箱 服 务 器  
        Properties props = new Properties();  
        //开 启debug调试   
        props.setProperty("mail.debug", "true");   
        //stmp服务器需要进行身份验证,也就是有 户 名和密 码的校验,这样才能通过验证  
        props.setProperty("mail.smtp.auth", "true");   
        //发送邮件协议名称   
        props.setProperty("mail.transport.protocol", "smtp");   
        //设置邮件服务器主机名   
        props.setProperty("mail.host", "smtp.163.com");//设置成163的发件服务器  

        //设端口号(该配置可写可不写)  
        props.setProperty("mail.smtp.port", port);  

        //密 码、授 权 码  
        props.setProperty("mail.smtp.password",password);  

        //Authenticator:认证信息  
        Session session = Session.getInstance(props, new Authenticator(){  
            @Override  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(userName,password);//使用它给其他账户发邮件  
            }  
        });  

        //2、创建邮件对象  
        Message message = new MimeMessage(session);  
        //2.1设置发件人  
        message.setFrom(new InternetAddress(userName));  
        //2、2设置收件人  
        message.setRecipient(RecipientType.TO, new InternetAddress(to));  
        //2.3邮件的主题  
        message.setSubject("测试发消息");  
        //2.4邮件的正文(即邮件的内容)  
        message.setContent("2018-1-30:测试邮件:javaMail-网 易 邮 箱测试","text/html;charset=utf-8");  

        //3.发送邮 件  
        Transport trans = session.getTransport();  
        //连接邮 件服务器  
        trans.connect(userName, password);   
        //发送邮 件  
        trans.sendMessage(message, message.getAllRecipients());   
        //关 闭连接  
        trans.close();  

        //Transport.send(message);(两种方式都可以)  
        System.out.println("发送成功");  
    }  

    public static void main(String[] args) {  
        try {  
            sendMain(userName2);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
    }  
        
}


发送QQ邮箱的工具类
[Java] 纯文本查看 复制代码
import java.security.GeneralSecurityException;
import java.util.Properties;

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

import com.sun.mail.util.MailSSLSocketFactory;

public class MailUtils_qq {
        
        private static String userName = "发件人的QQ邮箱地址";//发 送邮 件的Q Q邮 箱 账 号  
    private static String password = "发件人的QQ的密码";//授 权 码  
    private static String userName2 = "收件人的邮箱地址";//接 收 邮 件 的Q Q邮 箱 账 号  
    private static String port = "465";//465、587  
    /**
     * 该 方 法 用 来 发 送 邮 件
     * @param to:给 谁 发 邮 件
     * **/  
    public static void sendMain(String to) throws AddressException, MessagingException, GeneralSecurityException{  
        //1、创 建 连 接 对 象,连 接 到 邮 箱 服 务 器  
        Properties props = new Properties();  
        //开 启 debug 调 试   
        props.setProperty("mail.debug", "true");   
        //stmp服 务 器 需 要 进 行 身 份 验 证,也 就 是 用  户 名 和 密 码 的 校 验,这 样 才 能 通 过 验 证  
        props.setProperty("mail.smtp.auth", "true");   
        //发 送 邮 件 协 议 名 称   
        props.setProperty("mail.transport.protocol", "smtp");   
        //设 置 邮 件服 务 器 主 机 名   
        props.setProperty("mail.host", "smtp.qq.com");//设 置 成 q q 的发 件 服 务 器:(不要使用smtp.exmail.qq.com)  
        //设 端 口 号 (该 配 置 可 写 可 不 写)  
        props.setProperty("mail.smtp.port", port);  
        //授 权 码  
        props.setProperty("mail.smtp.password",password);         

        //开 启 S S L 加 密,否 则 会 失 败  
        MailSSLSocketFactory sf = new MailSSLSocketFactory();  
        sf.setTrustAllHosts(true);  
        props.put("mail.smtp.ssl.enable", "true");  
        props.put("mail.smtp.ssl.socketFactory", sf);  

        //Authenticator:认 证 信 息  
        Session session = Session.getInstance(props, new Authenticator(){  
            @Override  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(userName,password);//使 用 它 给 其 他 账 户 发 邮 件  
            }  
        });  

        //2、创 建 邮 件 对 象  
        Message message = new MimeMessage(session);  
        //2.1 设 置 发 件 人  
        message.setFrom(new InternetAddress(userName));  
        //2、2 设 置 收 件 人  
        message.setRecipient(RecipientType.TO, new InternetAddress(to));  
        //2.3 邮 件 的 主 题  
        message.setSubject("测 试 发 消 息");  
        //2.4 邮 件 的 正 文(即 邮 件 的 内 容)  
        message.setContent("测 试 邮 件:javaMail-Q Q 邮 箱 测 试","text/html;charset=utf-8");  

        //3.发 送 邮 件  
        Transport trans = session.getTransport();  
        //连 接 邮 件 服 务 器  
        trans.connect(userName, password);   
        //发 送 邮 件  
        trans.sendMessage(message, message.getAllRecipients());   
        //关 闭 连 接  
        trans.close();  

        //Transport.send(message);(两 种 方 式 都 可 以)  
        System.out.println("发 送 成 功");  
    }  

    public static void main(String[] args) {  
        try {  
            sendMain(userName2);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }   
    }  
        
}








作者: 逆风TO    时间: 2018-3-2 09:42
感谢老师分享




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2