JavaMail邮件发送机制详解

网友投稿 332 2022-12-21


JavaMail邮件发送机制详解

这篇文章主要介绍了javaMail邮件发送机制详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

概念

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

应用场景

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

代码实现

普通方式一

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

jar包:mail:1.4.1

坐标:

javax.mail

mail

1.4.4

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("

//指定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文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

JavaMail的配置文件

true

true

0

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

JavaMail的配置文件

true

true

0

3.mail.properties文件:

mail.host=smtp.163.com

mail.username=@前面的用户名

mail.password=密码

mail.from=发件人邮箱全拼


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:通过实例解析Spring组合注解与元注解
下一篇:Spring实战之调用实例工厂方法创建Bean操作示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~