java发送邮件(纯文本和带附件)

public class TestMail {

//纯文本
@Test
public void fun()throws AddressException,MessagingException{
Properties prop=new Properties();
prop.setProperty("mail.host", "smtp.163.com");
prop.setProperty("mail.smtp.auth", "true");
Authenticator auth=new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("发件人的邮箱","发件人的密码" );
}
};
Session session=Session.getInstance(prop,auth);
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("发件人的邮箱"));
msg.setRecipients(RecipientType.TO, "收件人的邮箱");
msg.setSubject("啦啦啦!!!");//标题
msg.setContent("啦啦啦!", "text/html;charset=utf-8"); //内容
Transport.send(msg);
}
//带附件的
@Test
public void fun2()throws AddressException,MessagingException,IOException{
Properties prop=new Properties();
prop.setProperty("mail.host", "smtp.163.com");
prop.setProperty("mail.smtp.auth", "true");
Authenticator auth=new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("发件人的邮箱","发件人的密码" );
}
};
Session session = Session.getInstance(prop, auth);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("发件人的邮箱"));
msg.setRecipients(RecipientType.TO,"收件人的邮箱");
msg.setSubject("测试邮件"); //标题
MimeMultipart list = new MimeMultipart();
MimeBodyPart part1 = new MimeBodyPart();
part1.setContent("啦啦啦!", "text/html;charset=utf-8"); //内容
list.addBodyPart(part1);
MimeBodyPart part2 = new MimeBodyPart();
part2.attachFile("D:\\title_en.png"); //附件
part2.setFileName(MimeUtility.encodeText("title_en.png"));
list.addBodyPart(part2);
msg.setContent(list);
Transport.send(msg);
}
}

转载于:https://www.cnblogs.com/leixia/p/8438590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JavaMail API来发送附件的邮件。下面是一个示例代码: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmailWithAttachment { public static void main(String[] args) { final String username = "your_email_address"; final String password = "your_email_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from_address")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to_address")); message.setSubject("Testing Subject"); // 创建一个多部分消息 Multipart multipart = new MimeMultipart(); // 创建文本消息部分 MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("This is message body"); // 添加文本消息部分到多部分消息 multipart.addBodyPart(messageBodyPart); // 创建附件部分 MimeBodyPart attachPart = new MimeBodyPart(); // 读取附件文件 String filename = "attachment_file_path"; DataSource source = new FileDataSource(filename); attachPart.setDataHandler(new DataHandler(source)); attachPart.setFileName("attachment_file_name"); // 添加附件部分到多部分消息 multipart.addBodyPart(attachPart); // 将多部分消息设置为消息内容 message.setContent(multipart); // 发送消息 Transport.send(message); System.out.println("邮件已发送"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 在代码中,你需要替换以下变量: - `your_email_address`:你的邮箱地址 - `your_email_password`:你的邮箱密码 - `from_address`:发件人邮箱地址 - `to_address`:收件人邮箱地址 - `attachment_file_path`:附件文件路径 - `attachment_file_name`:附件文件名 注意:在使用JavaMail API发送邮件时,你需要提供你所使用的邮件服务的SMTP服务器地址和端口号。在上面的代码中,我使用的是Gmail的SMTP服务器,如果你使用的是其他邮件服务,请替换相应的SMTP服务器地址和端口号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值