JavaMail实现邮件发送机制
更新时间:2022年8月11日 13:47 点击:192 作者:Mr小林
概念
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><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分配权限原理分析.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>
2.mail.properties文件:
mail.host=smtp.163.com mail.username=@前面的用户名 mail.password=密码 mail.from=发件人邮箱全拼
JavaMail与Spring整合点击即可进入
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
原文出处:https://blog.csdn.net/mrlin6688/article/details/53495703
上一篇: Java编写网络聊天程序实验
相关文章
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- 这篇文章主要为大家详细介绍了C++实现含附件的邮件发送功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
phpmailer邮件发送实例(163邮箱 126邮箱 yahoo邮箱)
phpmailer是一个非常优秀的php邮箱发送插件了,他可以几乎实现任何邮箱登录发送,下面我介绍163邮箱 126邮箱 yahoo邮箱的发送方法。 准备工作: 我们必须注册一个邮...2016-11-25- 用的mail()函数,感觉不好用,而且感觉这个邮件地址不能太多,一次copy了100个可能会出问题,下面提供一款可以限制发送邮件php类函数。 */ ...2016-11-25
- 利用php教程 mail()进行邮件发送实现方法 mail() 函数允许您从脚本中直接发送电子邮件。 如果邮件的投递被成功地接收,则返回 true,否则返回 false。 语法 mail(to,subj...2016-11-25
- phpmailer多邮件发送 require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->From = "list@mydomain.com"; $mail->FromName = "List manager"; $ma...2016-11-25
- 这篇文章主要为大家详细介绍了C# Email邮件发送功能,找回或重置密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 代码如下 复制代码 $pdfname="test.pdf"; $email="test@test.com"; $text = "您好,附件中是您需要的pdf文件。请点击下载。<br><a href=http://...2016-11-25
- 本款邮件发送功能我们是用了国外一个开源码的邮件类,大家都可能用过的PHPMailer邮件类很简单,今天来讲一下简单的使用教程,有需要的朋友可以参考下,同时像其它的附件什么...2016-11-25
- class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $us...2016-11-25
- 本篇文章对使用Lumisoft.NET实现邮件发送功能的方法机型了详细的分析介绍。需要的朋友参考下...2021-09-22
- 本文章介绍了关于在thinkphp中利用了phpmailer来实现邮件发送的详细教程,有需要的朋友可以参考一下。 本文所使用的是ThinkPHP 2.1版和 PHPMailer 5.1版。(后者建议...2016-11-25
- 这两天写了个方法用到php mail()本以为php提供的这个方法非常简单、方便、易用,其实不然! php mail()函数需要配置才可以发邮件的哦,下面配置方法 如果你没有使用win...2016-11-25
- phpmailer是一款免费的php邮件发送插件,我们可以利用phpmailer登录到我们指定的邮件然后再利用这个邮件给我们自动发邮件了,这个现在免费的邮箱像163,sina都支持,只是sin...2016-11-25
- PHPMailer是php中的一个邮件发送插件,下面我来给大家介绍PHPMailer邮件发送程序代码与在使用PHPMailer邮件发送时的一些常见问题解决办法总结。 PHPMailer是一个用...2016-11-25
- 这篇文章主要介绍了解决JavaMail附件名字过长导致的乱码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-10-11
- PHP邮件发送例子我介绍过很多不过几乎都是使用phpmailer邮件插件来实现发送了,下面这个例子我是测试发送邮件成功的例子了哦。 在win下, 利用PHP的mail函数来发送...2016-11-25