javaweb实现发送邮件

第一步:创建一个maven项目 然后确定项目名字
在这里插入图片描述
在pom.xml配置文件中添加依赖

     <!--       Mail邮件发送jar-->
    <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>

在这里插入图片描述
创建一个发送存文本类 (记得改为自己的邮箱账号和授权码)
public class MailText {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty(“mail.host”,“smtp.qq.com”);//设置邮箱服务器
properties.setProperty(“mail.transport.protocol”,“smtp”);//邮箱发送协议
properties.setProperty(“mail.smtp.auth”,“true”);//需要设置验证用户密码

    //关于qq邮箱,还要设置ssl加密,加上以下代码
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    properties.put("mail.smtp.ssl.enable","true");
    properties.put("mail.smtp.ssl.sockedFactory",sf);

    //使用JavaMail发送邮件的5个步骤
    //1、创建定义整个应用程序所需要的环境信息的Session 对象
    //创建定义整个应用程序所需要的环境信息的Session对象
    //QQ才有
    Session session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            //发送人信息和授权码
            return new PasswordAuthentication("qq号@qq.com","授权码");
        }
    });

    //开启Session的消息信息
    session.setDebug(true);

    //2.通过session得到transport对象
    Transport ts = session.getTransport();
    //3.使用邮箱的用户名和授权码连上邮件服务器
    ts.connect("smtp.qq.com","qq号@qq.com","授权码");
    //4.创建邮件
    //这里需要传递session,从session中的知信息从哪里来
    MimeMessage message = new MimeMessage(session);
    //指明邮件的发送人
    message.setFrom(new InternetAddress("qq号@qq.com"));
    //指明邮件的收件人  这里收件人是自己,自己发给自己
    message.setRecipient(Message.RecipientType.TO,new InternetAddress("qq号@qq.com"));
    //邮件标题
    message.setSubject("只包含文本文件的简单邮件");
    //邮件内容
    message.setContent("<h1 style='color:red'>你好啊!</h1>","text/html;charset=UTF-8");
    //5.发送邮件
    ts.sendMessage(message, message.getAllRecipients());

    //6.关闭连接
    ts.close();
}
}

qq的授权码需要在qq邮箱里的设置里面打开服务后获得
在这里插入图片描述
第二种:带有图片的邮件发送
public class MailPicture {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty(“mail.host”,“smtp.qq.com”);//设置邮箱服务器
properties.setProperty(“mail.transport.protocol”,“smtp”);//邮箱发送协议
properties.setProperty(“mail.smtp.auth”,“true”);//需要设置验证用户密码

    //关于qq邮箱,还要设置ssl加密,加上以下代码
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    properties.put("mail.smtp.ssl.enable","true");
    properties.put("mail.smtp.ssl.sockedFactory",sf);

    //使用JavaMail发送邮件的5个步骤
    //1、创建定义整个应用程序所需要的环境信息的Session 对象
    //创建定义整个应用程序所需要的环境信息的Session对象

    //QQ才有
    Session session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            //发送人信息和授权码
            return new PasswordAuthentication("qq号@qq.com","授权码");
        }
    });

    //开启Session的消息信息
    session.setDebug(true);

    //2.通过session得到transport对象
    Transport ts = session.getTransport();
    //3.使用邮箱的用户名和授权码连上邮件服务器
    ts.connect("smtp.qq.com","qq号@qq.com","授权码");
    //4.创建邮件
    //这里需要传递session,从session中的知信息从哪里来
    MimeMessage message = new MimeMessage(session);
    //指明邮件的发送人
    message.setFrom(new InternetAddress("qq号@qq.com"));
    //指明邮件的收件人  这里收件人是自己,自己发给自己
    message.setRecipient(Message.RecipientType.TO,new InternetAddress("qq号@qq.com"));
    //邮件标题
    message.setSubject("包含图片的文本");
    //准备图片数据
    MimeBodyPart image = new MimeBodyPart();
    //图片需要经过数据处理... DataHandler:数据处理
    DataHandler dh = new DataHandler(new FileDataSource("src/main/java/litter_spider.png"));
    image.setDataHandler(dh);//把图片数据传进去
    image.setContentID("litter_spider.png");//给图片设置一个ID,可以在后面使用

    //准备正文数据
    MimeBodyPart text = new MimeBodyPart();     //cid就是;ContentID上面设置的
    text.setContent("这是一封邮件正文带图片<img src='cid:litter_spider.png'>的邮件","text/html;charset=UTF-8");//邮件内容

    //描述数据关系
    MimeMultipart mm = new MimeMultipart();
    mm.addBodyPart(text);
    mm.addBodyPart(image);//将图片和文本都放进去
    mm.setSubType("related");//设置 数据类型为 related  包含图片的文本

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

    //5.发送邮件
    ts.sendMessage(message, message.getAllRecipients());

    //6.关闭连接
    ts.close();
}
}

其实就是增加了处理图片的操作
在这里插入图片描述
纯文本的数据类型是alternative,附带图片的文本为related,还加入了附件的话为mixed,实际上是类似拼接发送,先是纯文本和图片拼接在一起,然后在将 附件 和 拼接后的结果 进行拼接,然后一起发送
在这里插入图片描述

在这里插入图片描述
也可以将收件人邮箱设置为一个变量
在这里插入图片描述
可以直接从前端获取数据然后传入这里实现 发送给邮箱信息或者验证码的功能
详细的讲解可以看bilibili狂神解说的javaweb

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值