java代码发送邮件

该博客介绍了如何在Java应用程序中使用Apache Commons Email库和SpringBoot配置发送邮件。通过添加Maven依赖,配置SMTP服务器信息,如QQ邮箱的smtp.qq.com,设置发件人、收件人、主题和内容,以及实现邮件发送的代码。邮件内容包括验证码,并提供了邮箱格式的正则表达式验证。
摘要由CSDN通过智能技术生成

1.java发送邮件:

maven依赖:

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
        </dependency>

配置:

mail:
  #smtp服务主机  qq邮箱则为smtp.qq.com,126邮箱为smtp.126.com,163邮箱为163.smtp.com
  smtp:
    auth: true        
    host: smtp.qq.com
  user: 邮箱
  password: 邮箱授权码
  from: XXX网
  subject: XXX网会员邮箱注册验证码
  content: 尊敬用户:<br>&nbsp;&nbsp;你好!XXX网会员邮箱注册验证码为:%code%,十分钟内有效。
  verify:
    code:  
      length: 6
  regex: ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

代码

    @Value("${mail.smtp.auth}")
    private String mailSmtpAuth;
    @Value("${mail.smtp.host}")//发送邮件的服务器 126邮箱为smtp.126.com,163邮箱为163.smtp.com,QQ为smtp.qq.com
    private String mailSmtpHost;
    @Value("${mail.user}")//发件人邮箱地址
    private String mailUser;
    @Value("${mail.password}")//发件人邮箱客户端授权码
    private String mailPassword;
    @Value("${mail.subject}")//邮件主题
    private String mailSubject;
    @Value("${mail.content}")//邮件内容
    private String mailContent;
    @Value("${mail.verify.code.length}")
    private String vcodeLength;//验证码长度
    @Value("${mail.regex}")
    private String mailRegex;//邮箱格式正则表达式

    /**
     * 发送邮箱
     * @param to
     * @param content
     * @throws MessagingException
     */
    private void sendMail(String to, String content) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.auth", mailSmtpAuth);
        props.put("mail.smtp.host", mailSmtpHost);
        props.put("mail.user", mailUser);// 发件人的账号
        props.put("mail.password", mailPassword);//发件人的密码
        Authenticator authenticator = new Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(mailUser, mailPassword);
            }
        };
        //创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        MimeMessage message = new MimeMessage(mailSession);// 创建邮件消息
        message.setFrom(new InternetAddress(mailUser));// 设置发件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));// 设置收件人
        message.setSubject(mailSubject); // 设置邮件标题
        message.setContent(content, "text/html;charset=UTF-8"); // 设置邮件的内容体
        Transport.send(message);// 发送邮件
    }

2.SpringBoot发送邮件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java代码发送邮件需要使用JavaMail API和Java Activation Framework(JAF)。 以下是使用JavaMail API和JAF发送邮件的步骤: 1. 下载JavaMail API和JAF库,并将它们添加到项目的类路径中。 2. 创建一个邮件会话对象: ```java Properties props = new Properties(); props.put("mail.smtp.host", "smtp.example.com"); // SMTP服务器地址 Session session = Session.getInstance(props, null); ``` 3. 创建一个MimeMessage对象,设置发件人、收件人、主题和内容: ```java MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("Test Email"); message.setText("This is a test email."); ``` 4. 发送邮件: ```java Transport.send(message); ``` 完整的Java代码示例: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail { public static void main(String[] args) { String smtpHost = "smtp.example.com"; String from = "sender@example.com"; String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email."; Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); Session session = Session.getInstance(props, null); try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); message.setText(text); Transport.send(message); System.out.println("Email sent successfully."); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值