• java邮件发送


    //发送一封简单的邮件
    public class MailDemo01 {
        public static void main(String[] args) throws Exception {
            Properties prop = new Properties();
            prop.setProperty("mail.host","stmp.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.stmp.ssl.enable","true");
            prop.put("mail.stmp.ssl.socketFactory",sf);
            //使用JavaMail发送邮件的五个步骤
            //1.创建定义整个应用程序所需要的环境信息的Session对象
    
            //QQ才有!其他邮箱不用
            Session session= Session.getDefaultInstance(prop, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    //发送人邮箱有户名、授权码
                    return new PasswordAuthentication("邮箱","授权码");
                }
            });
            //开启Session的debug模式,查看发送运行状态
            session.setDebug(true);
            //2.通过session得到transport对象
            Transport ts = session.getTransport();
            //3.使用邮箱的用户名和授权码连上邮件服务器
            ts.connect("smtp.qq.com","邮箱","授权码");
            //4.创建邮件
            //注意需要传递Session
            MimeMessage message = new MimeMessage(session);
            //指明邮件的发件人
            message.setFrom(new InternetAddress("邮箱"));
    
            //指明邮件的收件人
            message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("邮箱")});
    
            //邮件的标题
            message.setSubject("只包含文本的简单邮件");
            /*
            //准备图片数据
        MimeBodyPart image = new MimeBodyPart();
         //图片需要经过数据处理 DataHandler:数据处理
          DataHandler dh=new DataHandler(new FileDataSource("src/resources/bz.jpg"));
          image.setDataHandler(dh);//在我们的body主体中放入这个处理的图片
          image.setContentID("bz.jpg");//设置图片的ID
         //准备正文数据
          MimeBodyPart text=new MimeBodyPart();
          text.setContent("这是一封正文带图片<image src='cid:bz.jpg>的邮件","text/html;charset=UTF-8");
          //描述数据关系
          MimeMultipart mm=new MimeMultipart();
          mm.addBodyPart(text);
          mm.addBodyPart(image);
          mm.setSubType("related");
        //设置到消息中,保存修改
          message.setContent(mm);
          message.saveChanges();
         */
            //内容
            message.setContent("<h1 style='color:red'>你好啊</h1>","text/html;charset=UTF-8");
            //5.发送邮件
            ts.sendMessage(message,message.getAllRecipients());
            //6.关闭连接
            ts.close();
        }
    }

    注意:使用前需要导入相应的jar包

              mail.jar

              activation.jar

    想了解全部内容请去关注狂神说

  • 相关阅读:
    主流浏览器内核概览
    图片圆角边框自适应宽高(深夜原创)
    <程序员节>
    谁说Float菜单不可以水平居中
    如何做好一份前端工程师的简历?
    重温textjustify:interideograph
    Firefox 4 beta 1发布——前端开发者须知
    让PHP程序永远在后台运行
    如何让Linux后台运行命令或php
    PHP执行后台程序 argv
  • 原文地址:https://www.cnblogs.com/ws-sharecode/p/12792119.html
Copyright © 2020-2023  润新知