亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2022年08月11日 10:04:21   作者:Mr小林  
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(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àng),也可以用此方法實(shí)現(xiàn)用戶的激活。

代碼實(shí)現(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.測試代碼實(shí)現(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("項(xiàng)目進(jìn)展順序");

? ? ? ? //7.設(shè)置郵件的正文
? ? ? ? message.setText("項(xiàng)目進(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)文章

最新評論