最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能

 更新時(shí)間:2022年06月22日 11:02:14   作者:深藍(lán)夢(mèng)夕陽(yáng)  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程詳解

    SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了SpringBoot封裝響應(yīng)數(shù)據(jù)實(shí)現(xiàn)過程,SpringBoot響應(yīng)數(shù)據(jù)封裝是指在SpringBoot應(yīng)用程序中,將返回的數(shù)據(jù)進(jìn)行封裝,以便于前端頁(yè)面或其他客戶端使用,感興趣想要詳細(xì)了解可以參考下文
    2023-05-05
  • Mysql中備份表的多種方法

    Mysql中備份表的多種方法

    本文給大家分享Mysql中備份表的四種方法,第一種方式是小表的備份,第二種是對(duì)整個(gè)數(shù)據(jù)庫(kù)的備份與恢復(fù),第三種是對(duì)某個(gè)數(shù)據(jù)表進(jìn)行備份,每種方式給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • java常用工具類 UUID、Map工具類

    java常用工具類 UUID、Map工具類

    這篇文章主要為大家詳細(xì)介紹了Java常用工具類,包括UUID工具類、Map工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • SpringBoot RESTful風(fēng)格入門講解

    SpringBoot RESTful風(fēng)格入門講解

    RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口
    2022-11-11
  • java經(jīng)典問題:連個(gè)字符串互為回環(huán)變位

    java經(jīng)典問題:連個(gè)字符串互為回環(huán)變位

    連個(gè)字符串互為回環(huán)變位經(jīng)常出現(xiàn)在java程序員面試中,這個(gè)是考驗(yàn)程序員的解題思路和方法的最經(jīng)典的一題,小編為大家詳細(xì)分析一下,一起來學(xué)習(xí)吧。
    2017-11-11
  • Java8中Function接口的使用方法詳解

    Java8中Function接口的使用方法詳解

    在 Java 8 中,Function 接口是 java.util.function 包中的一個(gè)函數(shù)式接口,函數(shù)式接口是僅包含一個(gè)抽象方法的接口,適用于 Lambda 表達(dá)式或方法引用,本文給大家介紹了Java8的Function接口的使用方法,需要的朋友可以參考下
    2024-09-09
  • spring?mybatis環(huán)境常量與枚舉轉(zhuǎn)換示例詳解

    spring?mybatis環(huán)境常量與枚舉轉(zhuǎn)換示例詳解

    這篇文章主要為大家介紹了spring?mybatis環(huán)境常量與枚舉轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • JAVA面試題 簡(jiǎn)談你對(duì)synchronized關(guān)鍵字的理解

    JAVA面試題 簡(jiǎn)談你對(duì)synchronized關(guān)鍵字的理解

    這篇文章主要介紹了JAVA面試題 請(qǐng)談?wù)勀銓?duì)Sychronized關(guān)鍵字的理解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案

    macOS上使用gperftools定位Java內(nèi)存泄漏問題及解決方案

    這篇文章主要介紹了macOS上使用gperftools定位Java內(nèi)存泄漏問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 關(guān)于Idea卡在Resolving Maven dependencies的解決方案

    關(guān)于Idea卡在Resolving Maven dependencies的解決方案

    本文詳細(xì)介紹了關(guān)于Idea卡在Resolving Maven dependencies的解決方案,文中通過圖文結(jié)合的形式給大家介紹的非常詳細(xì),對(duì)大家解決問題有一定的幫助,需要的朋友可以參考下
    2024-02-02

最新評(píng)論

霍林郭勒市| 万州区| 禄劝| 扎赉特旗| 固始县| 文山县| 哈尔滨市| 电白县| 惠州市| 藁城市| 临潭县| 天全县| 会宁县| 甘德县| 德惠市| 通海县| 宁海县| 和平区| 五寨县| 忻城县| 永丰县| 镇巴县| 舞阳县| 兴山县| 衡阳县| 舒城县| 连南| 民丰县| 宁陵县| 社旗县| 汪清县| 林西县| 泰安市| 新田县| 勃利县| 西青区| 新邵县| 冀州市| 古浪县| 长子县| 项城市|