在Java中发送带附件的邮件可以使用JavaMail api来实现。以下是一个示例代码: import java.util.Prop
在Java中发送带附件的邮件可以使用JavaMail api来实现。以下是一个示例代码:
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
public class EmailSender {
public static void main(String[] args) {
// 发件人邮箱和密码
final String username = "your_email@example.com";
final String password = "your_password";
// 发件人邮箱的 SMTP 服务器地址
final String smtpHost = "smtp.example.com";
// 收件人邮箱
String toEmail = "recipient@example.com";
// 创建邮件会话
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 创建邮件消息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
message.setSubject("JavaMail Attachment Example");
// 创建邮件内容
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("This is a sample email with attachment.");
// 附件文件路径
String attachmentPath = "/path/to/attachment.txt";
// 创建附件
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(source.getName());
// 将内容和附件添加到邮件中
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentBodyPart);
message.setContent(multipart);
// 发送邮件
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
在上述代码中,你需要替换以下内容:
your_email@example.com
:发件人邮箱your_password
:发件人邮箱密码smtp.example.com
:发件人邮箱的 SMTP 服务器地址recipient@example.com
:收件人邮箱/path/to/attachment.txt
:附件的文件路径确保你已经添加了javax.mail
和activation
依赖,以及正确配置了邮件服务器信息。
--结束END--
本文标题: java发送邮件带附件怎么实现
本文链接: https://www.lsjlt.com/news/570492.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0