Java實(shí)現(xiàn)郵件發(fā)送QQ郵箱帶附件
本文實(shí)例為大家分享了Java實(shí)現(xiàn)郵件發(fā)送QQ郵箱帶附件的具體代碼,供大家參考,具體內(nèi)容如下
添加依賴
<!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
關(guān)鍵代碼
import java.io.File; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; 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.mail.internet.MimeUtility; /** * 郵件發(fā)送工具類 <br/> * Author:楊杰超<br/> * Date:2020年1月9日 下午12:02:51 <br/> * Copyright (c) 2020, yangjiechao@dingtalk.com All Rights Reserved.<br/> * */ public class SendMail { /** * 想QQ郵箱發(fā)送郵件 * * @param formMail * 發(fā)送人郵箱地址 * @param descMail * 接收人郵箱地址 * @param subject * 郵箱主題 * @param content * 郵箱內(nèi)容 * @param files * 附件列表 * @param contentType * 內(nèi)容格式 * @param password * SMTP密碼 * @throws MessagingException * @throws UnsupportedEncodingException */ public static void sendQQMail(String formMail, String descMail, String subject, String content, File[] files, String contentType, String password) throws MessagingException, UnsupportedEncodingException { Properties properties = new Properties(); properties.setProperty("mail.smtp.host", "smtp.qq.com"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.debug", "true"); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.ssl.enable", "true"); Session session = Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(formMail, password); } }); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(formMail)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(descMail)); message.setSubject(subject); // 是否存在附件 if (null != files && files.length > 0) { MimeMultipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content, contentType); multipart.addBodyPart(contentPart); for (File file : files) { MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource(file)); attachment.setDataHandler(dh2); attachment.setFileName(MimeUtility.encodeText(dh2.getName())); multipart.addBodyPart(attachment); } multipart.setSubType("mixed"); message.setContent(multipart); message.saveChanges(); } // 普通 else { message.setContent(content, contentType); } Transport transport = session.getTransport(); transport.connect(formMail, password); Transport.send(message); } catch (UnsupportedEncodingException e) { throw e; } catch (NoSuchProviderException e) { throw e; } catch (MessagingException e) { throw e; } } public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { // 由哪個(gè)郵箱發(fā)送 String formMail = "********@qq.com"; // QQ郵箱>設(shè)置>賬戶 開啟POP3/SMTP服務(wù) 查看smtp密碼 String smtpPassword = "****************"; // 發(fā)送人郵箱地址 String descMail = "470947852@qq.com"; String contentType = "text/html;charset=UTF-8"; String subject = "測(cè)試郵件發(fā)送,含附件"; String content = "test send mail, 這里是中文"; File[] files = new File[2]; files[0] = new File("C:/test_1.xls"); files[1] = new File("C:/test_2.xls"); SendMail.sendQQMail(formMail, descMail, subject, content, files, contentType, smtpPassword); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程池FutureTask實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java線程池FutureTask實(shí)現(xiàn)原理詳解,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02搭建簡(jiǎn)單的Spring-Data JPA項(xiàng)目
本文主要介紹了搭建簡(jiǎn)單的Spring-Data JPA項(xiàng)目,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Dubbo Service Mesh基礎(chǔ)架構(gòu)組件改造
Service Mesh這個(gè)“熱”詞是2016年9月被“造”出來(lái),而今年2018年更是被稱為service Mesh的關(guān)鍵之年,各家大公司都希望能在這個(gè)思潮下領(lǐng)先一步2023-03-03完美解決springboot中使用mybatis字段不能進(jìn)行自動(dòng)映射的問(wèn)題
今天在springboot中使用mybatis的時(shí)候不能字段不能夠進(jìn)行自動(dòng)映射,接下來(lái)給大家給帶來(lái)了完美解決springboot中使用mybatis字段不能進(jìn)行自動(dòng)映射的問(wèn)題,需要的朋友可以參考下2023-05-05SpringBoot實(shí)現(xiàn)啟動(dòng)項(xiàng)目后立即執(zhí)行的方法總結(jié)
在項(xiàng)目開發(fā)中某些場(chǎng)景必須要用到啟動(dòng)項(xiàng)目后立即執(zhí)行方式的功能,所以這篇文章就來(lái)和大家聊聊實(shí)現(xiàn)立即執(zhí)行的幾種方法,希望對(duì)大家有所幫助2023-05-05flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo
這篇文章主要為大家介紹了flowable動(dòng)態(tài)創(chuàng)建多級(jí)流程模板實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05