java发送邮件
发布日期:2021-05-08 13:44:37 浏览次数:24 分类:精选文章

本文共 3333 字,大约阅读时间需要 11 分钟。

以下是关于QQ邮箱和163邮箱邮件发送功能的实现代码示例,供开发者参考使用。代码基于JavaMail库,通过设置相应的SMTP服务器参数实现邮件发送功能。


1. QQ邮箱邮件发送示例

package com.yang.travel.util;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;public class QQMailUtil {    public static void sendMail(String toEmail, String emailMsg) throws Exception {        Properties props = new Properties();        props.put("mail.smtp.auth", "true");        props.put("mail.smtp.host", "smtp.qq.com");        props.put("mail.smtp.port", "587");        props.put("mail.user", "115xxxx61@qq.com");        props.put("mail.password", "xxxx");                Authenticator authenticator = new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(props.getProperty("mail.user"), props.getProperty("mail.password"));            }        };                Session mailSession = Session.getInstance(props, authenticator);        MimeMessage message = new MimeMessage(mailSession);                InternetAddress form = new InternetAddress(props.getProperty("mail.user"));        message.setFrom(form);        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail));        message.setSubject("测试邮件");        message.setContent(emailMsg, "text/html;charset=UTF-8");                Transport.send(message);    }        public static void main(String[] args) throws Exception {        String toEmail = "159xxxx19@163.com";        String emailMsg = "测试一下";        sendMail(toEmail, emailMsg);        System.out.println("邮件发送成功!");    }}

2. 163邮箱邮件发送示例

package com.yang.travel.util;import javax.mail.*;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Properties;public class MailUtil {    public static void sendMail(String toEmail, String emailMsg) throws Exception {        Properties props = new Properties();        props.put("mail.smtp.host", "smtp.163.com");        props.put("mail.smtp.auth", "true");                Authenticator auth = new Authenticator() {            @Override            public PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication("159xxxx9", "1991yangenwang");            }        };                Session session = Session.getInstance(props, auth);        MimeMessage message = new MimeMessage(session);        message.setFrom(new InternetAddress("159xxxx9@163.com"));        message.setRecipient(MimeAddress.RecipientType.TO, new InternetAddress(toEmail));        message.setSubject("用户激活");        message.setContent(emailMsg, "text/html;charset=UTF-8");                Transport.send(message);    }        public static void main(String[] args) throws Exception {        String toEmail = "115xxxx61@qq.com";        String emailMsg = "测试一下";        sendMail(toEmail, emailMsg);        System.out.println("邮件发送成功!");    }}

3. 使用说明

  • QQ邮箱配置

    • SMTP服务器地址:smtp.qq.com
    • SMTP端口:587
    • 账户名:您的QQ邮箱地址(例如:115xxxx61@qq.com
    • 授权密码:您在QQ客户端设置的16位授权码
  • 163邮箱配置

    • SMTP服务器地址:smtp.163.com
    • SMTP端口:994
    • 账户名:您的163邮箱地址(例如:159xxxx9@163.com
    • 授权密码:您的163邮箱授权码(例如:1991yangenwang

4. 注意事项

  • 授权密码:请确保在使用前设置好QQ邮箱或163邮箱的客户端授权密码。如果您忘记授权密码,可以重新设置客户端授权密码,并在代码中使用新的授权码。
  • 接收邮箱:发送邮件的toEmail参数请替换为实际收件人的邮箱地址。
  • 邮件内容emailMsg参数请根据实际需求设置邮件内容。
上一篇:java自定义异常
下一篇:js中两种定时器,setTimeout和setInterval实现验证码发送

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月18日 13时22分24秒