温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用JavaMail怎么实现邮件发送机制

发布时间:2021-06-11 16:12:24 来源:亿速云 阅读:238 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关使用JavaMail怎么实现邮件发送机制,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

概念

JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook的应用程序。

应用场景

一般在系统的注册模块,当用户填入注册信息的邮箱时,点击保存。系统根据用户的信息会自动给用户发送一封邮件,上面有用户的基本信息和注意事项,也可以用此方法实现用户的激活。

代码实现

普通方式一

1.首先引入javaMail的mail坐标即jar包

jar包:mail:1.4.1

坐标:

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

2.测试代码实现

首先引入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.设置邮件的一些信息     Properties props = new Properties();     //发送邮件的服务器地址     props.put("mail.smtp.host", "smtp.163.com");// stmp.qq.com  smtp.sina.com     props.put("mail.smtp.auth", "true");     //2.创建Session对象     Session session =Session.getInstance(props);     //3.创建出MimeMessage,邮件的消息对象     MimeMessage message = new MimeMessage(session);     //4.设置发件人     Address fromAddr = new InternetAddress("发件人邮箱");     message.setFrom(fromAddr);     //5.设置收件人     Address toAddr = new InternetAddress("收件人邮箱");     message.setRecipient(RecipientType.TO, toAddr);     //6.设置邮件的主题     message.setSubject("项目进展顺序");     //7.设置邮件的正文     message.setText("项目进展顺序,所有兄弟们都非常努力,老板今天可以请吃饭");     message.saveChanges();//保存更新     //8.得到火箭     Transport transport = session.getTransport("smtp");     transport.connect("smtp.163.com", "发件人邮箱", "发件人密码");//设置了火箭的发射地址     transport.sendMessage(message, message.getAllRecipients());//发送具体内容及接收人        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");     //得到发送器     JavaMailSender mailSender = (JavaMailSender) ac.getBean("mailSender");     //得到一个MimeMessage对象     MimeMessage message = mailSender.createMimeMessage();     //产生出一个MimeMessageHelper helper      MimeMessageHelper helper = new MimeMessageHelper(message, true);//工具类本质是操作message消息  true代表可以带附件,图片     //3.使用helper工具类,设置邮件的发送者,接收者,主题,正文     helper.setFrom("发送邮箱");     helper.setTo("接收邮箱");     helper.setSubject("发送图片和附件");     helper.setText("<html><head></head><body><h2>hello!!spring image html mail</h2><a href=http://www.baidu.com>baidu</a><img src='cid:image' /></body></html>", true);     //指定cid的取值     FileSystemResource imgResource = new FileSystemResource(new File("E:/01分配权限原理分析.png"));     helper.addInline("image", imgResource);     //带附件     FileSystemResource fileResource = new FileSystemResource(new File("E:/javamail1_4_4.zip"));     helper.addAttachment("javamail1_4_4.zip", fileResource);     //发送     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"/>   <!-- 简单消息对象创建 -->   <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">      <property name="from" value="${mail.from}"></property>   </bean>   <!-- 2.创建发送器 -->     <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>

3.mail.properties文件:

mail.host=smtp.163.com mail.username=@前面的用户名 mail.password=密码 mail.from=发件人邮箱全拼

以上就是使用JavaMail怎么实现邮件发送机制,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI