java发送邮件到foxmail

        以下代码是我用通义千问测试的,大家可以参考一下:

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail {

    public static void main(String[] args) {

        // 设置发件人邮箱地址和密码
        String from = "你的邮箱地址";
        String password = "你的邮箱密码";

        // 设置收件人邮箱地址
        String to = "收件人1,收件人2,收件人3";

        // 设置抄送人邮箱地址
        String cc = "抄送人1,抄送人2,抄送人3";

        // 设置邮件主题和正文
        String subject = "测试邮件";
        String body = "这是一封测试邮件。";

        // 创建一个新的邮件对象
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        try {
            // 创建一个新的邮件对象
            Message message = new MimeMessage(session);

            // 设置邮件发件人、收件人、抄送人、主题和正文
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            message.setSubject(subject);
            message.setText(body);

            // 发送邮件
            Transport.send(message);

            System.out.println("邮件发送成功!");

        } catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java基础,Java开发,Java,web开发,java,foxmail,python,java-ee,spring,boot)