javax.mail发送邮件(带附件)
最近公司开发项目,需要有一个发送带附件的邮件的功能,因此上网查找资料后实现了该功能,在这里将整个开发过程与大家分享,大家共同进步,有不足之处,请多多指教。
一.配置QQ邮箱:
需要对作为发件人的QQ邮箱进行相关配置,这样才能够保证使用的邮箱能够被正常使用,配置过程如下:
(1)打开QQ邮箱,登陆后,点击“设置”,在切换至“账户”,找到下图所示的位置。
(2.)点击“开启”后,验证密保,验证完成后,会得到一个授权码,该授权码在接下来的实际开发中需要用到,请及时记录下来,如下图所示,到此,QQ邮箱配置已完成。
二.实际开发:
(1.)jar包配置:需要如下所示的jar包,版本号随意。
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail --> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.14.RELEASE</version> </dependency>
(2.)实际开发代码如下:
public static void main(String[] args) throws Exception { Properties props = new Properties(); //可以加载一个配置文件 // 使用smtp:简单邮件传输协议 props.put("mail.smtp.host", "smtp.qq.com");//存储发送邮件服务器的信息 props.put("mail.smtp.auth", "true");//同时通过验证 Session session = Session.getInstance(props);//根据属性新建一个邮件会话 // session.setDebug(true); //有他会打印一些调试信息。 MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象 message.setFrom(new InternetAddress("**************@qq.com"));//设置发件人的地址 message.setRecipient(Message.RecipientType.TO, new InternetAddress("**************@qq.com"));//设置收件人,并设置其接收类型为TO message.setSubject("22232232222");//设置标题 //设置信件内容 // message.setText(mailContent); //发送 纯文本 邮件 todo //message.setContent("232323232323", "text/html;charset=gbk"); //发送HTML邮件,内容样式比较丰富 message.setSentDate(new Date());//设置发信时间 // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 Multipart multipart = new MimeMultipart(); // 设置邮件的文本内容 BodyPart contentPart = new MimeBodyPart(); contentPart.setText("邮件的具体内容在此"); multipart.addBodyPart(contentPart); // 添加附件 BodyPart messageBodyPart = new MimeBodyPart(); DataSource dataSource = new FileDataSource(new File("D:\\intentionDescription\\20180514153827170.png")); // 添加附件的内容 messageBodyPart.setDataHandler(new DataHandler(dataSource)); // 添加附件的标题 // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码 messageBodyPart.setFileName(MimeUtility.encodeText(dataSource.getName())); multipart.addBodyPart(messageBodyPart); // 将multipart对象放到message中 message.setContent(multipart); message.saveChanges();//存储邮件信息 //发送邮件 Transport transport = session.getTransport(); transport.connect("**************@qq.com", "jiqaggzvjrzyhjdd");//发送邮件的账号, 授权码 transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址 transport.close(); } )