java 邮箱发送 待附件
时间: 2023-09-03 21:13:06 浏览: 52
好的,您需要使用JavaMail API来发送带附件的电子邮件。以下是一个示例代码,您可以根据自己的需求进行修改:
```
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 EmailSender {
public static void main(String[] args) {
// 收件人的电子邮件ID
String to = "recipient@example.com";
// 发件人的电子邮件ID
String from = "sender@example.com";
// 发件人电子邮件的用户名和密码
final String username = "sender_username";
final String password = "sender_password";
// 定义邮件服务器的主机名和端口号
String host = "smtp.example.com";
int port = 587;
// 创建一个邮件会话
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
// 创建一个默认的 MimeMessage 对象
Message message = new MimeMessage(session);
// 设置 From: 头部头字段
message.setFrom(new InternetAddress(from));
// 设置 To: 头部头字段
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
// 设置主题
message.setSubject("邮件主题");
// 创建一个多部分消息
Multipart multipart = new MimeMultipart();
// 创建文本消息部分
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("邮件正文");
multipart.addBodyPart(messageBodyPart);
// 创建附件部分
messageBodyPart = new MimeBodyPart();
String filename = "path/to/file";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 设置消息多部分的内容
message.setContent(multipart);
// 发送消息
Transport.send(message);
System.out.println("邮件已经发送。");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
```
在代码中,您需要替换以下内容:
- to:收件人的电子邮件ID
- from:发件人的电子邮件ID
- username:发件人电子邮件的用户名
- password:发件人电子邮件的密码
- host:邮件服务器的主机名
- port:邮件服务器的端口号
- message.setSubject("邮件主题"):设置邮件的主题
- messageBodyPart.setText("邮件正文"):设置邮件的正文
- filename:附件的路径
请确保您已经在您的项目中添加了 JavaMail API。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)