Java的qq邮箱发送案例

1、首先开启qq邮箱账户的发送邮件的许可

 

默认状态是关闭状态,点击开启会让你发送固定语句到指定账户,成功后会生成一个授权码,记住你的授权码

2、发送邮箱代码

sendEmail

public class SendEmail {

    public static void sendRegisterEmail(String username,String code,String email){

        try {
            //创建一个配置文件并保存
            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.socketFactory", sf);

            //创建一个session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    //配置发送人右键的账号和授权码
                    return new PasswordAuthentication("发送人的qq@qq.com", "发送人的授权码");
                }
            });

            //开启debug模式
            session.setDebug(true);

            //获取连接对象
            Transport transport = session.getTransport();

            //连接服务器
            //配置发送人右键的账号和授权码
            transport.connect("smtp.qq.com", "发送人的qq@qq.com", "发送人的授权码");

            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);

            //邮件发送人
            mimeMessage.setFrom(new InternetAddress("发送人的qq@qq.com"));

            //邮件接收人
//            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的qq@qq.com"));


            String content="<a href='http://locahost:8080/test/ActivationServlet?username=\"+username+\"&code=\"+code+'>点击激活</a>";

            //邮件标题
            mimeMessage.setSubject("注册邮件");

            //邮件内容
            mimeMessage.setContent(content, "text/html;charset=UTF-8");

            //发送邮件
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

            //关闭连接
            transport.close();

        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("发送注册邮件是失败!");
        }
    }


    public static void main(String[] args) {

//测试发送邮件
        SendEmail.sendRegisterEmail("zhangsan","0098",null);
    }

}

3、邮件常用案例

普通的发送邮件业务、账号激活业务、修改密码业务等。

4、邮箱需要用到的jar包

由于上传太麻烦了,可以自己百度

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,以下是 Java 发送定时邮件的代码示例: ```java import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class EmailScheduler { public static void main(String[] args) { // 收件人的电子邮件 ID String to = "[email protected]"; // 发件人的电子邮件 ID String from = "[email protected]"; // 发件人的电子邮件密码 String password = "password_here"; // SMTP 主机 String host = "smtp.qq.com"; // 获取系统的属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 使用 TLS(Transport Layer Security)加密传输 properties.put("mail.smtp.starttls.enable", "true"); // 设置邮件服务器需要授权认证 properties.setProperty("mail.smtp.auth", "true"); // 获取默认的 Session 对象 Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建一个默认的 MimeMessage 对象并设置相关属性 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("Test Email from Java"); // 设置邮件正文 message.setText("Hello, this is a test email sent from Java."); // 设置定时时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date time = calendar.getTime(); // 创建定时任务 Timer timer = new Timer(); // 发送邮件任务 TimerTask task = new TimerTask() { public void run() { try { // 发送邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { System.out.println("Error occurred while sending email. " + e.getMessage()); } } }; // 定时发送邮件 timer.schedule(task, time); } catch (MessagingException e) { System.out.println("Error occurred while preparing email. " + e.getMessage()); } } } ``` 希望这个代码可以帮到您。如有其他问题,欢迎随时问我。而当您问我前面对我说了什么时,我可以告诉您一个笑话。为什么熬夜对皮肤不好?因为你的脸似乎很喜欢黏键盘。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值