已复制
全屏展示
复制代码

Java发送邮件完整示例


· 2 min read

一. pom依赖

<dependency>
  <groupId>com.sun.mail</groupId>
  <artifactId>javax.mail</artifactId>
  <version>1.6.2</version>
</dependency>

或者在官网下载 jar 包 https://javaee.github.io/javamail/

二. 发送邮件示例

下面是网易 yeah.net 邮箱的示例。

package org.example;

import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;


public class Main {
    public static void main(String[] args) {
        String[] toEmails = {"xxx@qq.com"};
        String from = "username@yeah.net";
        String fromPassword = "xxxx";
        String host = "smtp.yeah.net";

        Properties properties = System.getProperties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.timeout", "10000") // 超时 毫秒


        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, fromPassword);
            }
        });
        // 开启debug模式
        session.setDebug(true);

        try {
            InternetAddress[] toEmailsArray = new InternetAddress[toEmails.length];
            for (int i = 0; i < toEmails.length; i++) {
                toEmailsArray[i] = new InternetAddress(toEmails[i]);
            }

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipients(Message.RecipientType.TO, toEmailsArray);
            message.setSubject("邮件的主题");

            // 构建 multipart 对象
            Multipart multipart = new MimeMultipart();

            // 添加正文
            BodyPart textPart = new MimeBodyPart();
            textPart.setContent("邮件正文。。", "text/plain;charset=utf-8");
            multipart.addBodyPart(textPart);

            // 添加附件
            String[] files = {"/Users/yzy/.vimrc", "/Users/yzy/.bash_profile"};
            for (String f : files) {
                File file = new File(f);
                BodyPart filePart = new MimeBodyPart();
                filePart.setFileName(file.getName());
                filePart.setDataHandler(new DataHandler(new FileDataSource(file)));
                multipart.addBodyPart(filePart);
            }

            // 添加 multipart 并发送
            message.setContent(multipart);
            Transport.send(message);

        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}
🔗

文章推荐