javaweb学习28:邮件发送原理及实现

  • 邮件发送:
  • 流程图:
  • 要在网络上实现邮件功能,必须要有专门的邮件服务器
  • 这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中;
  • SMTP服务器地址:一般是smtp.xxx.com;比如:163邮箱是:smtp.163.com;qq邮箱是:smtp.qq.com
     
  • Java实现邮件发送:
  • 首先要准备JavaMail API 和 Java Activation Framework
  • 得到2个包:
mail.jar
activation.jar
  • javaMail是sun公司为Java开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发包,它支持一些常用的邮件协议:SMTP,POP3,IMAP,MIME(附件)等,我们在使用JavaMail API 编写邮件时,无需考虑邮件的底层实现细节,只要调用javaMail开发包中相应的API类就可以了;
  • MIME:图片,附件;

 

  • MIME:多用途互联网邮件扩展类型(附件)
  • MimeBodyPart类
  • MimeMultipart类:
  • alternative:文本
  • related:图片
  • mixed:附件

 

  • 代码案例:纯文本邮件
//发送纯文本邮箱
public class MailDemo01 {
    public static void main(String[] args) throws Exception {

        Properties prop=new Properties();
        prop.setProperty("mail.host","smtp.qq.com");//设置QQ邮箱服务器
        prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
        prop.setProperty("mail.smtp.auth","true");//需要验证用户名和密码


        //关于QQ邮箱,还要设置SSL加密,加上以下代码即可
        MailSSLSocketFactory sf=new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);


        //使用javamail发送邮件的5个步骤


        //1,创建定义整个应用程序所需的环境信息的Session对象;
        //QQ才有,其他邮箱不需要
        Session session=Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发送人邮件用户名,授权码
                return new PasswordAuthentication("XXX@qq.com","XXX");//授权码
            }
        });
        //开启Session的DEBUG模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2,通过session得到transport对象
        Transport ts=session.getTransport();
        //3,使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com","XXX@qq.com","XXX");
        //4,创建邮件:写邮件
        MimeMessage message = new MimeMessage(session);//注意需要传递session
        //发送人
        message.setFrom(new InternetAddress("XXX@qq.com"));
        //收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("XXX@qq.com"));
        //邮件主体
        message.setSubject("纯文本邮件");
        //邮件内容
        message.setContent("<h1>你好啊,我是H1</h1>","text/html;charset=utf-8");

        //5,发送
        ts.sendMessage(message,message.getAllRecipients());
        //6,关闭连接
        ts.close();

    }
}
  • 代码案例:带图片的邮件
//带附件的邮件
public class MailDemo02 {
    public static void main(String[] args) throws Exception {

        Properties prop=new Properties();
        prop.setProperty("mail.host","smtp.qq.com");//设置QQ邮箱服务器
        prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
        prop.setProperty("mail.smtp.auth","true");//需要验证用户名和密码


        //关于QQ邮箱,还要设置SSL加密,加上以下代码即可
        MailSSLSocketFactory sf=new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable","true");
        prop.put("mail.smtp.ssl.socketFactory",sf);


        //使用javamail发送邮件的5个步骤


        //1,创建定义整个应用程序所需的环境信息的Session对象;
        //QQ才有,其他邮箱不需要
        Session session=Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发送人邮件用户名,授权码
                return new PasswordAuthentication("xx@qq.com","xx");//授权码
            }
        });
        //开启Session的DEBUG模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2,通过session得到transport对象
        Transport ts=session.getTransport();
        //3,使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com","xx@qq.com","xx");
        //4,创建邮件:写邮件
        MimeMessage message = new MimeMessage(session);//注意需要传递session
        //发送人
        message.setFrom(new InternetAddress("xx@qq.com"));
        //收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress("xx@qq.com"));
        //邮件主体
        message.setSubject("带图片的邮件");

        //准备图片数据
        MimeBodyPart image=new MimeBodyPart();
        //图片需要经过数据处理
        DataHandler dh = new DataHandler(new FileDataSource("D:\\1.PNG"));
        image.setDataHandler(dh);
        image.setContentID("1.png");

        //准备正文数据
        MimeBodyPart text=new MimeBodyPart();
        text.setContent("你好啊,我是<img src='cid:1.png'>图片","text/html;charset=utf-8");

        //描述数据关系
        MimeMultipart mm=new MimeMultipart();
        mm.addBodyPart(text);
        mm.addBodyPart(image);
        mm.setSubType("related");

        //设置到消息中,保存修改
        message.setContent(mm);
        message.saveChanges();

        //5,发送