欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaMail實現(xiàn)郵件發(fā)送機(jī)制

 更新時間:2022年08月11日 10:04:21   作者:Mr小林  
這篇文章主要為大家詳細(xì)介紹了JavaMail實現(xiàn)郵件發(fā)送機(jī)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

概念

JavaMail,顧名思義,提供給開發(fā)者處理電子郵件相關(guān)的編程接口。它是Sun發(fā)布的用來處理email的API。它可以方便地執(zhí)行一些常用的郵件傳輸。我們可以基于JavaMail開發(fā)出類似于Microsoft Outlook的應(yīng)用程序。

應(yīng)用場景

一般在系統(tǒng)的注冊模塊,當(dāng)用戶填入注冊信息的郵箱時,點(diǎn)擊保存。系統(tǒng)根據(jù)用戶的信息會自動給用戶發(fā)送一封郵件,上面有用戶的基本信息和注意事項,也可以用此方法實現(xiàn)用戶的激活。

代碼實現(xiàn)

普通方式一

1.首先引入javaMail的mail坐標(biāo)即jar包

jar包:mail:1.4.1

坐標(biāo):

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>

2.測試代碼實現(xiàn)

首先引入junit 測試包

package mail.test;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.junit.Test;

public class Mail01Test {

? ? @Test
? ? public void testJavaMail() throws Exception{
? ? ? ? //1.設(shè)置郵件的一些信息
? ? ? ? Properties props = new Properties();
? ? ? ? //發(fā)送郵件的服務(wù)器地址
? ? ? ? props.put("mail.smtp.host", "smtp.163.com");// ?stmp.qq.com ? smtp.sina.com
? ? ? ? props.put("mail.smtp.auth", "true");

? ? ? ? //2.創(chuàng)建Session對象
? ? ? ? Session session =Session.getInstance(props);

? ? ? ? //3.創(chuàng)建出MimeMessage,郵件的消息對象
? ? ? ? MimeMessage message = new MimeMessage(session);

? ? ? ? //4.設(shè)置發(fā)件人
? ? ? ? Address fromAddr = new InternetAddress("發(fā)件人郵箱");
? ? ? ? message.setFrom(fromAddr);

? ? ? ? //5.設(shè)置收件人
? ? ? ? Address toAddr = new InternetAddress("收件人郵箱");
? ? ? ? message.setRecipient(RecipientType.TO, toAddr);

? ? ? ? //6.設(shè)置郵件的主題
? ? ? ? message.setSubject("項目進(jìn)展順序");

? ? ? ? //7.設(shè)置郵件的正文
? ? ? ? message.setText("項目進(jìn)展順序,所有兄弟們都非常努力,老板今天可以請吃飯");
? ? ? ? message.saveChanges();//保存更新

? ? ? ? //8.得到火箭
? ? ? ? Transport transport = session.getTransport("smtp");
? ? ? ? transport.connect("smtp.163.com", "發(fā)件人郵箱", "發(fā)件人密碼");//設(shè)置了火箭的發(fā)射地址
? ? ? ? transport.sendMessage(message, message.getAllRecipients());//發(fā)送具體內(nèi)容及接收人 ? ??
? ? ? ? transport.close();
? ? }

}

普通方式二

方式二可以帶附件和圖片

1.測試代碼:

package mail.test;
import java.io.File;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class Mail03Test {

? ? @Test
? ? public void testJavaMail() throws Exception{
? ? ? ? ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-mail.xml");

? ? ? ? //得到發(fā)送器
? ? ? ? JavaMailSender mailSender = (JavaMailSender) ac.getBean("mailSender");

? ? ? ? //得到一個MimeMessage對象
? ? ? ? MimeMessage message = mailSender.createMimeMessage();

? ? ? ? //產(chǎn)生出一個MimeMessageHelper helper?
? ? ? ? MimeMessageHelper helper = new MimeMessageHelper(message, true);//工具類本質(zhì)是操作message消息 ? true代表可以帶附件,圖片

? ? ? ? //3.使用helper工具類,設(shè)置郵件的發(fā)送者,接收者,主題,正文
? ? ? ? helper.setFrom("發(fā)送郵箱");
? ? ? ? helper.setTo("接收郵箱");
? ? ? ? helper.setSubject("發(fā)送圖片和附件");
? ? ? ? helper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1><a href=http://www.baidu.com>baidu</a><img src='cid:image' /></body></html>

//指定cid的取值
? ? ? ? FileSystemResource imgResource = new FileSystemResource(new File("E:/01分配權(quán)限原理分析.png"));
? ? ? ? helper.addInline("image", imgResource);

? ? ? ? //帶附件
? ? ? ? FileSystemResource fileResource = new FileSystemResource(new File("E:/javamail1_4_4.zip"));
? ? ? ? helper.addAttachment("javamail1_4_4.zip", fileResource);

? ? ? ? //發(fā)送
? ? ? ? mailSender.send(message);

? ? }
}

2.applicationContext-mail.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ?
? ? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? ??
? ? xmlns:p="http://www.springframework.org/schema/p" ?
? ? xmlns:context="http://www.springframework.org/schema/context" ??
? ? xmlns:tx="http://www.springframework.org/schema/tx" ?
? ? xmlns:aop="http://www.springframework.org/schema/aop" ?
? ? xsi:schemaLocation="http://www.springframework.org/schema/beans ? ?
? ? http://www.springframework.org/schema/beans/spring-beans.xsd ? ?
? ? http://www.springframework.org/schema/aop ? ?
? ? http://www.springframework.org/schema/aop/spring-aop.xsd ? ?
? ? http://www.springframework.org/schema/tx ? ?
? ? http://www.springframework.org/schema/tx/spring-tx.xsd ? ?
? ? http://www.springframework.org/schema/context ? ?
? ? http://www.springframework.org/schema/context/spring-context.xsd">

? ? <description>JavaMail的配置文件</description>
? ? <!-- 加載mail.properties配置文件 -->
? ? <context:property-placeholder location="classpath:mail.properties"/>


? ? <!-- 簡單消息對象創(chuàng)建 -->
? ? <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
? ? ? ? ?<property name="from" value="${mail.from}"></property>
? ? </bean>

? ? <!-- 2.創(chuàng)建發(fā)送器 --> ? ?
? ? <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
? ? ? ? ?<property name="host" value="${mail.host}"></property>
? ? ? ? ?<property name="username" value="${mail.username}"></property>
? ? ? ? ?<property name="password" value="${mail.password}"></property>
? ? ? ? ?<property name="defaultEncoding" value="UTF-8"></property>
? ? ? ? ?<property name="javaMailProperties">
? ? ? ? ? ? <props>
? ? ? ? ? ? ? ? ?<prop key="mail.smtp.auth">true</prop>
? ? ? ? ? ? ? ? ?<prop key="mail.debug">true</prop>
? ? ? ? ? ? ? ? ?<prop key="mail.smtp.timeout">0</prop>
? ? ? ? ? ? </props>
? ? ? ? ?</property>
? ? </bean>
</beans>

2.mail.properties文件:

mail.host=smtp.163.com
mail.username=@前面的用戶名
mail.password=密碼
mail.from=發(fā)件人郵箱全拼

JavaMail與Spring整合點(diǎn)擊即可進(jìn)入

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密的代碼

    java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密的代碼

    這篇文章給大家分享java使用RSA加密方式實現(xiàn)數(shù)據(jù)加密解密,通過實例代碼文字相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下
    2019-11-11
  • Java 使用getClass().getResourceAsStream()方法獲取資源

    Java 使用getClass().getResourceAsStream()方法獲取資源

    這篇文章主要介紹了Java 使用getClass().getResourceAsStream()方法獲取資源的相關(guān)資料,這里主要講解哪種方式可以獲取到文件資源,需要的朋友可以參考下
    2017-07-07
  • 關(guān)于遠(yuǎn)程調(diào)用RestTemplate的使用避坑指南

    關(guān)于遠(yuǎn)程調(diào)用RestTemplate的使用避坑指南

    這篇文章主要介紹了關(guān)于遠(yuǎn)程調(diào)用RestTemplate的使用避坑指南,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java格式化時間示例

    java格式化時間示例

    這篇文章主要介紹了java格式化時間示例,需要的朋友可以參考下
    2014-04-04
  • SpringBoot+WebSocket實現(xiàn)即時通訊的方法詳解

    SpringBoot+WebSocket實現(xiàn)即時通訊的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用SpringBoot+WebSocket實現(xiàn)即時通訊功能,文中示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定參考價值,需要的可以參考一下
    2022-05-05
  • Java實現(xiàn)簡單的遞歸操作方法實例

    Java實現(xiàn)簡單的遞歸操作方法實例

    這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)簡單的遞歸操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • java微信開發(fā)第二步 獲取消息和回復(fù)消息

    java微信開發(fā)第二步 獲取消息和回復(fù)消息

    這篇文章主要為大家分享java微信開發(fā)的第二步,如何獲取消息和回復(fù)消息,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Java語言Iterator轉(zhuǎn)換成 List的方法

    Java語言Iterator轉(zhuǎn)換成 List的方法

    在 Java 中,迭代器(Iterator)是一種用于遍歷集合中元素的對象,它提供了一種簡單而一致的方式來訪問集合中的元素,而不需要暴露集合內(nèi)部的結(jié)構(gòu),這篇文章主要介紹了Java語言Iterator轉(zhuǎn)換成 List的方法,需要的朋友可以參考下
    2023-08-08
  • spring-AOP 及 AOP獲取request各項參數(shù)操作

    spring-AOP 及 AOP獲取request各項參數(shù)操作

    這篇文章主要介紹了spring-AOP 及 AOP獲取request各項參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 淺析Spring的JdbcTemplate方法

    淺析Spring的JdbcTemplate方法

    本篇淺析Spring的JdbcTemplate方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論