Java 群发邮箱

此次发送邮箱一共用到了一个jar

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

代码如下,把发送人邮箱地址、授权码以及收件人邮箱地址更换后就可以正常用了

package com.example.security.zmain.emailMain;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;

/**
 * 群发邮箱
 */
public class GroupSendEmail {
    public static void main(String[] args) throws MessagingException, MessagingException {

        Properties props = new Properties();
        // 开启debug调试 
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名 qq邮箱就是smtp.qq.com
        props.setProperty("mail.host", "smtp.163.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");
        // 设置环境信息
        Session session = Session.getInstance(props);
        // 创建邮件对象
        Message msg = new MimeMessage(session);

        //标题
        msg.setSubject("验证码");
        // 设置邮件内容
        //不带样式的内容
        msg.setText("你的验证码为:"+UUID.randomUUID());
        //设置带样式的内容
        //msg.setContent("<H1>你的验证码为111:</H1>"+UUID.randomUUID(), "text/html;charset=utf-8");
        // 设置发件人
        msg.setFrom(new InternetAddress("发送人邮箱@163.com"));

        Transport transport = session.getTransport();
        // 连接邮件服务器
        String username = "发送人邮箱@163.com";//发送邮箱账号
        String userpassword = "邮箱授权码";//发送邮箱密码 这个不是邮箱的密码 ,这个是邮箱stmp服务的授权码
        transport.connect(username, userpassword);
        // 发送邮件 两种方式 收件人邮箱没有限制,什么邮箱都可以
//        transport.sendMessage(msg, new Address[] {new InternetAddress("邮箱@qq.com"),new InternetAddress("邮箱@qq.com")});
        transport.sendMessage(msg, InternetAddress.parse("收件人邮箱@qq.com,收件人邮箱@qq.com"));
        // 关闭连接
        transport.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,实现邮箱群发和发送多个附件可以使用 JavaMail API。下面是一个简单的示例代码: ```java import java.io.File; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class EmailService { private String host; // SMTP服务器主机名 private String username; // 发件人账号 private String password; // 发件人密码 public EmailService(String host, String username, String password) { this.host = host; this.username = username; this.password = password; } public void sendEmail(String[] to, String[] cc, String[] bcc, String subject, String content, String[] attachments) throws Exception { Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.ssl.enable", "true"); Authenticator auth = new EmailAuthenticator(username, password); Session mailSession = Session.getInstance(props, auth); MimeMessage message = new MimeMessage(mailSession); message.setFrom(new InternetAddress(username)); InternetAddress[] toAddresses = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { toAddresses[i] = new InternetAddress(to[i]); } message.setRecipients(Message.RecipientType.TO, toAddresses); if (cc != null && cc.length > 0) { InternetAddress[] ccAddresses = new InternetAddress[cc.length]; for (int i = 0; i < cc.length; i++) { ccAddresses[i] = new InternetAddress(cc[i]); } message.setRecipients(Message.RecipientType.CC, ccAddresses); } if (bcc != null && bcc.length > 0) { InternetAddress[] bccAddresses = new InternetAddress[bcc.length]; for (int i = 0; i < bcc.length; i++) { bccAddresses[i] = new InternetAddress(bcc[i]); } message.setRecipients(Message.RecipientType.BCC, bccAddresses); } message.setSubject(subject); MimeMultipart contentPart = new MimeMultipart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(content); contentPart.addBodyPart(textPart); if (attachments != null && attachments.length > 0) { for (String attachment : attachments) { MimeBodyPart attachPart = new MimeBodyPart(); File file = new File(attachment); attachPart.attachFile(file); attachPart.setFileName(MimeUtility.encodeText(file.getName())); contentPart.addBodyPart(attachPart); } } message.setContent(contentPart); Transport.send(message); } private static class EmailAuthenticator extends Authenticator { private String username; private String password; public EmailAuthenticator(String username, String password) { this.username = username; this.password = password; } @Override protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(username, password); } } } ``` 然后就可以在你的代码中使用这个 EmailService 类来发送邮件了: ```java EmailService emailService = new EmailService("smtp.example.com", "username@example.com", "password"); String[] to = {"recipient1@example.com", "recipient2@example.com"}; String[] cc = {"cc1@example.com", "cc2@example.com"}; String[] bcc = {"bcc1@example.com", "bcc2@example.com"}; String subject = "Test email"; String content = "This is a test email."; String[] attachments = {"/path/to/attachment1", "/path/to/attachment2"}; emailService.sendEmail(to, cc, bcc, subject, content, attachments); ``` 其中,`to`、`cc`、`bcc` 分别表示邮件的收件人、抄送人、密送人,`subject` 表示邮件主题,`content` 表示邮件内容,`attachments` 表示邮件的附件路径数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值