Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送的具體代碼,供大家參考,具體內(nèi)容如下
需要的jar包:
- activation-1.1.1.jar
- mail-1.4.7.jar
QQ郵箱設(shè)置開啟POP3/SMTP服務(wù),并獲得授權(quán)碼
java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
import com.sun.mail.util.MailSSLSocketFactory; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class Mail1 { ? ? public static ?void main(String[] args) throws Exception { ? ? ? ? //要發(fā)送郵件,需要獲得協(xié)議和支持!開啟服務(wù)POP3/SMTP服務(wù) ?授權(quán)碼: fsxqgovorymigfeb ? ? ? ? Properties prop=new Properties(); ? ? ? ? prop.setProperty("mail.host","smtp.qq.com");//設(shè)置QQ郵件服務(wù)器 ? ? ? ? prop.setProperty("mail.transport.protocol","smtp");//設(shè)置郵箱發(fā)送協(xié)議 ? ? ? ? prop.setProperty("mail.smtp.auth","true");//需要驗(yàn)證用戶名密碼 ? ? ? ? //QQ郵箱還有設(shè)置SSL加密 ? ? ? ? MailSSLSocketFactory sf=new MailSSLSocketFactory(); ? ? ? ? sf.setTrustAllHosts(true); ? ? ? ? prop.put("mail.smtp.ssl.enable","true"); ? ? ? ? prop.put("mail.smtp.ssl.socketFactory",sf); ? ? ? ? //1.創(chuàng)建定義整個(gè)應(yīng)用程序所需要的環(huán)境信息的Session對(duì)象 ? ? ? ? Session session=Session.getDefaultInstance(prop, new Authenticator() { ? ? ? ? ? ? @Override ? ? ? ? ? ? protected PasswordAuthentication getPasswordAuthentication() { ? ? ? ? ? ? ? ? return new PasswordAuthentication("1369410772@qq.com","fsxqgovorymigfeb"); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //開啟session的debug模式,這樣就可以查看運(yùn)行狀態(tài)了 ? ? ? ? session.setDebug(true); ? ? ? ? //2.通過session對(duì)象獲得transport對(duì)象 ? ? ? ? Transport transport = session.getTransport(); ? ? ? ? //3.使用郵箱的用戶名和授權(quán)碼連上郵件服務(wù)器 ? ? ? ? transport.connect("smtp.qq.com","1369410772@qq.com","fsxqgovorymigfeb"); ? ? ? ? //4.創(chuàng)建郵件:寫郵件 ? ? ? ? MimeMessage message = new MimeMessage(session); ? ? ? ? message.setFrom(new InternetAddress("1369410772@qq.com"));//發(fā)件人 ? ? ? ? message.setRecipient(Message.RecipientType.TO,new InternetAddress("1369410772@qq.com"));//收件人 ? ? ? ? message.setSubject("你好");//郵件主題 ? ? ? ? message.setContent("<h1 style='color: red'>你好</h1>","text/html;charset=utf-8");//郵件內(nèi)容 ? ? ? ? //5.發(fā)送郵件 ? ? ? ? transport.sendMessage(message,message.getAllRecipients()); ? ? ? ? //6.關(guān)閉連接 ? ? ? ? transport.close(); ? ? } }
java實(shí)現(xiàn)復(fù)雜郵件發(fā)送( 帶文件 )
import com.sun.mail.util.MailSSLSocketFactory; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Properties; public class Mail1 { ? ? public static ?void main(String[] args) throws Exception { ? ? ? ? //要發(fā)送郵件,需要獲得協(xié)議和支持!開啟服務(wù)POP3/SMTP服務(wù) ?授權(quán)碼: fsxqgovorymigfeb ? ? ? ? Properties prop=new Properties(); ? ? ? ? prop.setProperty("mail.host","smtp.qq.com");//設(shè)置QQ郵件服務(wù)器 ? ? ? ? prop.setProperty("mail.transport.protocol","smtp");//設(shè)置郵箱發(fā)送協(xié)議 ? ? ? ? prop.setProperty("mail.smtp.auth","true");//需要驗(yàn)證用戶名密碼 ? ? ? ? //QQ郵箱還有設(shè)置SSL加密 ? ? ? ? MailSSLSocketFactory sf=new MailSSLSocketFactory(); ? ? ? ? sf.setTrustAllHosts(true); ? ? ? ? prop.put("mail.smtp.ssl.enable","true"); ? ? ? ? prop.put("mail.smtp.ssl.socketFactory",sf); ? ? ? ? //1.創(chuàng)建定義整個(gè)應(yīng)用程序所需要的環(huán)境信息的Session對(duì)象 ? ? ? ? Session session=Session.getDefaultInstance(prop, new Authenticator() { ? ? ? ? ? ? @Override ? ? ? ? ? ? protected PasswordAuthentication getPasswordAuthentication() { ? ? ? ? ? ? ? ? return new PasswordAuthentication("1369410772@qq.com","fsxqgovorymigfeb"); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //開啟session的debug模式,這樣就可以查看運(yùn)行狀態(tài)了 ? ? ? ? session.setDebug(true); ? ? ? ? //2.通過session對(duì)象獲得transport對(duì)象 ? ? ? ? Transport transport = session.getTransport(); ? ? ? ? //3.使用郵箱的用戶名和授權(quán)碼連上郵件服務(wù)器 ? ? ? ? transport.connect("smtp.qq.com","1369410772@qq.com","fsxqgovorymigfeb"); ? ? ? ? //4.創(chuàng)建郵件:寫郵件 ? ? ? ? MimeMessage message = new MimeMessage(session); ? ? ? ? message.setFrom(new InternetAddress("1369410772@qq.com"));//發(fā)件人 ? ? ? ? message.setRecipient(Message.RecipientType.TO,new InternetAddress("1369410772@qq.com"));//收件人 ? ? ? ? message.setSubject("你好");//郵件主題 ? ? ? ? //message.setContent("<h1 style='color: red'>你好</h1>","text/html;charset=utf-8");//郵件內(nèi)容 ? ? ? ? //============================================================================= ? ? ? ? //帶圖片的內(nèi)容 ? ? ? ? MimeBodyPart image = new MimeBodyPart(); ? ? ? ? DataHandler dh = new DataHandler(new FileDataSource("E:\\IDEA\\JavaWeb\\mail-java\\src\\tx.png"));//圖片需要經(jīng)過數(shù)據(jù)處理... DataHandler:數(shù)據(jù)處理 ? ? ? ? image.setDataHandler(dh);//在Body中放入處理的圖片數(shù)據(jù) ? ? ? ? image.setContentID("tx.png");//給圖片設(shè)置ID ? ? ? ? //準(zhǔn)備正文數(shù)據(jù) ? ? ? ? MimeBodyPart text = new MimeBodyPart(); ? ? ? ? text.setContent("這是一封郵件正文帶圖片<img src='cid:tx.png'>的郵件","text/html;charset=utf-8"); ? ? ? ? //描述數(shù)據(jù)關(guān)系 ? ? ? ? MimeMultipart mm = new MimeMultipart(); ? ? ? ? mm.addBodyPart(text); ? ? ? ? mm.addBodyPart(image); ? ? ? ? mm.setSubType("mixed"); ? ? ? ? //設(shè)置到消息中,保存修改 ? ? ? ? message.setContent(mm); ? ? ? ? message.saveChanges(); ? ? ? ? //========================================================================= ? ? ? ? //5.發(fā)送郵件 ? ? ? ? transport.sendMessage(message,message.getAllRecipients()); ? ? ? ? //6.關(guān)閉連接 ? ? ? ? transport.close(); ? ? } }
Spring實(shí)現(xiàn)
1、添加依賴
<dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、編寫配置文件
spring.mail.username=1369410772@qq.com spring.mail.password=fsxqgovorymigfeb spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true
3、編寫測(cè)試類
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @SpringBootTest class DemoApplicationTests {//簡(jiǎn)單郵件 ? ? @Autowired ? ? JavaMailSenderImpl mailSender; ? ? @Test ? ? void contextLoads() { ? ? ? ? //發(fā)送郵件 ? ? ? ? //收件人 ? ? ? ? //內(nèi)容 ? ? ? ? SimpleMailMessage message = new SimpleMailMessage(); ? ? ? ? message.setSubject("測(cè)試"); ? ? ? ? message.setText("Hello"); ? ? ? ? message.setFrom("1369410772@qq.com"); ? ? ? ? message.setTo("1369410772@qq.com"); ? ? ? ? mailSender.send(message); ? ? } ? ? @Test ? ? public void test2() throws Exception {//復(fù)雜郵件 ? ? ? ? MimeMessage mimeMessage = mailSender.createMimeMessage(); ? ? ? ? MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); ? ? ? ? helper.setSubject("測(cè)試"); ? ? ? ? helper.setText("Hello",true); ? ? ? ? //附件 ? ? ? ? helper.addAttachment("1.jpg",new File("")); ? ? ? ? helper.setFrom("1369410772@qq.com"); ? ? ? ? helper.setTo("1369410772@qq.com"); ? ? ? ? mailSender.send(mimeMessage); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java郵件發(fā)送程序(可以同時(shí)發(fā)給多個(gè)地址、可以帶附件)
- Java HtmlEmail 郵件發(fā)送的簡(jiǎn)單實(shí)現(xiàn)代碼
- 基于JavaMail的Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能
- Java實(shí)現(xiàn)帶附件的郵件發(fā)送功能
- JavaWeb實(shí)現(xiàn)郵件發(fā)送接收功能
- Java實(shí)現(xiàn)郵件發(fā)送功能
- Java實(shí)現(xiàn)郵件發(fā)送遇到的問題
- java郵件發(fā)送的實(shí)現(xiàn)
- java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼
- JavaMail實(shí)現(xiàn)郵件發(fā)送的方法
相關(guān)文章
SpringBoot利用限速器RateLimiter實(shí)現(xiàn)單機(jī)限流的示例代碼
本文主要介紹了SpringBoot利用限速器RateLimiter實(shí)現(xiàn)單機(jī)限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01java設(shè)計(jì)模式之簡(jiǎn)單工廠模式簡(jiǎn)述
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之簡(jiǎn)單工廠模式,簡(jiǎn)單工廠模式的實(shí)質(zhì)是由一個(gè)工廠類根據(jù)傳入的參數(shù),動(dòng)態(tài)決定應(yīng)該創(chuàng)建哪一個(gè)產(chǎn)品類的實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08hystrix配置中Apollo與Archaius對(duì)比分析
這篇文章主要為大家介紹了hystrix的配置中Apollo與Archaius對(duì)比分析,并為大家解答在hystrix的配置中有了Apollo是否還需要Archaius這一問題詳解2022-02-02Springboot插件開發(fā)實(shí)戰(zhàn)分享
這篇文章主要介紹了Springboot插件開發(fā)實(shí)戰(zhàn)分享,文章通過新建aop切面執(zhí)行類MonitorLogInterceptor展開詳細(xì)的相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05