java发送附件_Java发送邮件(带附件)

本文介绍了如何使用Java发送带有附件的电子邮件。通过JavaMail API,详细讲解了创建MimeMessage对象,设置发件人、收件人、主题,添加邮件正文和附件,以及进行SMTP验证和发送邮件的步骤。
摘要由CSDN通过智能技术生成

importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.DataSource;importjavax.activation.FileDataSource;importjavax.mail.BodyPart;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjavax.mail.internet.MimeUtility;public classJavaMailWithAttachment {privateMimeMessage message;privateSession session;privateTransport transport;private String mailHost = "";private String sender_username = "";private String sender_password = "";private Properties properties = newProperties();/** 初始化方法*/

public JavaMailWithAttachment(booleandebug) {

InputStream in= JavaMailWithAttachment.class.getResourceAsStream("MailServer.properties");try{

properties.load(in);this.mailHost = properties.getProperty("mail.smtp.host");this.sender_username = properties.getProperty("mail.sender.username");this.sender_password = properties.getProperty("mail.sender.password");

}catch(IOException e) {

e.printStackTrace();

}

session=Session.getInstance(properties);

session.setDebug(debug);//开启后有调试信息

message = newMimeMessage(session);

}/*** 发送邮件

*

*@paramsubject

* 邮件主题

*@paramsendHtml

* 邮件内容

*@paramreceiveUser

* 收件人地址

*@paramattachment

* 附件*/

public voiddoSendHtmlEmail(String subject, String sendHtml, String receiveUser, File attachment) {try{//发件人

InternetAddress from = newInternetAddress(sender_username);

message.setFrom(from);//收件人

InternetAddress to = newInternetAddress(receiveUser);

message.setRecipient(Message.RecipientType.TO, to);//邮件主题

message.setSubject(subject);//向multipart对象中添加邮件的各个部分内容,包括文本内容和附件

Multipart multipart = newMimeMultipart();//添加邮件正文

BodyPart contentPart = newMimeBodyPart();

contentPart.setContent(sendHtml,"text/html;charset=UTF-8");

multipart.addBodyPart(contentPart);//添加附件的内容

if (attachment != null) {

BodyPart attachmentBodyPart= newMimeBodyPart();

DataSource source= newFileDataSource(attachment);

attachmentBodyPart.setDataHandler(newDataHandler(source));//网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码//sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();//messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");//MimeUtility.encodeWord可以避免文件名乱码

attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));

multipart.addBodyPart(attachmentBodyPart);

}//将multipart对象放到message中

message.setContent(multipart);//保存邮件

message.saveChanges();

transport= session.getTransport("smtp");//smtp验证,就是你用来发邮件的邮箱用户名密码

transport.connect(mailHost, sender_username, sender_password);//发送

transport.sendMessage(message, message.getAllRecipients());

System.out.println("send success!");

}catch(Exception e) {

e.printStackTrace();

}finally{if (transport != null) {try{

transport.close();

}catch(MessagingException e) {

e.printStackTrace();

}

}

}

}public static voidmain(String[] args) {

JavaMailWithAttachment se= new JavaMailWithAttachment(true);

File affix= new File("c:\\测试-test.txt");

se.doSendHtmlEmail("邮件主题", "邮件内容", "xxx@XXX.com", affix);//}

}

带附件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值