21xrx.com
2025-03-30 16:39:01 Sunday
文章检索 我的文章 写文章
Java发送邮件附件教程
2023-07-05 00:33:15 深夜i     10     0
Java邮件 发送附件 教程

在我们日常的工作中,发送邮件是一个很常见的任务,而在Java程序中发送邮件也是非常容易实现的。除了发送文本邮件,有时候我们也需要为邮件添加附件。本文将介绍如何使用Java发送邮件附件。

首先我们需要导入JavaMail的jar包,如果你使用Maven,可以将以下依赖添加到pom.xml文件中:

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

在Java程序中发送邮件一般需要使用到以下几个类:

* javax.mail.Session:表示邮件会话,通过该类我们可以连接邮箱服务器,并设置邮件发送的相关参数。

* javax.mail.Message:表示邮件的消息体,包含邮件发送人、收件人、主题、内容等。

* javax.mail.Transport:表示邮件发送的传输对象,通过该类我们可以将邮件发送出去。

下面是通过Java发送邮件附件的步骤:

1. 创建Session对象:需传入SMTP服务器、用户名和密码等信息。

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() {
  public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("sender@qq.com", "password");
  }
});

2. 创建Message对象:设置邮件信息,包括邮件发送者、接收者、主题、内容等。

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@qq.com"));
message.setSubject("邮件主题");
// 创建邮件附件
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(new File("attachment.txt"));
// 设置邮件内容
Multipart multipart = new MimeMultipart();
MimeBodyPart contentPart = new MimeBodyPart();
contentPart.setContent("邮件正文", "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);

3. 发送邮件:调用Transport的send方法发送邮件。

Transport.send(message);

完整的Java发送邮件附件示例代码如下:

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class SendMailWithAttachment {
  public static void main(String[] args) throws IOException, MessagingException {
    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() {
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("sender@qq.com", "password");
      }
    });
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("sender@qq.com"));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@qq.com"));
    message.setSubject("邮件主题");
    // 创建邮件附件
    MimeBodyPart attachmentPart = new MimeBodyPart();
    attachmentPart.attachFile(new File("attachment.txt"));
    // 设置邮件内容
    Multipart multipart = new MimeMultipart();
    MimeBodyPart contentPart = new MimeBodyPart();
    contentPart.setContent("邮件正文", "text/html;charset=utf-8");
    multipart.addBodyPart(contentPart);
    multipart.addBodyPart(attachmentPart);
    message.setContent(multipart);
    Transport.send(message);
  }
}

希望本文对您学习Java发送邮件附件有所帮助。如果您还有其他问题,欢迎留言讨论。

  
  
下一篇: C++练手!

评论区

请求出错了