发邮件

发送邮件demo

发送邮件demo实例:

  • 此demo实例仅发送文本内容

代码块

代码块语法遵循标准markdown代码,例如:

public static void main(String[] args) throws Exception {
         Properties prop = new Properties();
         prop.setProperty("mail.host", "smtp.qq.com");//发件服务器
         prop.setProperty("mail.transport.protocol", "smtp");//邮件传输协议
         prop.setProperty("mail.smtp.auth", "true");//是否设置身份认证
         prop.setProperty("mail.smtp.host", "25");//发件服务器端口
         prop.setProperty("mail.smtp.encrytion", "ssl");//ssl认证
         prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         prop.setProperty("mail.smtp.socketFactory.port", "465");
         //使用JavaMail发送邮件的5个步骤
         //1、创建session
         Session session = Session.getInstance(prop);
         //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
         session.setDebug(true);
         //2、通过session得到transport对象
         Transport ts = session.getTransport();
         //3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
         ts.connect("smtp.qq.com", "发件人邮箱/用户名", "邮箱登录密码");
         //4、创建邮件
         Message message = createSimpleMail(session);
         //5、发送邮件
         ts.sendMessage(message, message.getAllRecipients());
         ts.close();
    }

    private static Message createSimpleMail(Session session) throws Exception {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //指明邮件的发件人
        message.setFrom(new InternetAddress("发件人邮箱"));
        //指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮箱"));
        //邮件的标题
        message.setSubject("找回密码");
        //邮件的文本内容
        message.setContent("发送内容", "text/html;charset=UTF-8");
        //返回创建好的邮件对象
        return message;
    }

脚注

生成一个脚注1.


  1. 这里是 脚注内容.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值