JavaMail邮件发送原理及步骤详细解析?

2026-06-09 06:583阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1502个文字,预计阅读时间需要7分钟。

JavaMail邮件发送原理及步骤详细解析?

本文主要介绍了JavaMail邮件发送机制,通过示例代码详细阐述了其使用方法。对于需要学习或工作的朋友,具有一定的参考价值。以下是对JavaMail的概念、命名意义和提供的示例的简要概述:

概念:JavaMail是一个Java API,用于发送和接收电子邮件。它提供了发送邮件、读取邮件、管理邮件服务器等功能。

命名意义:JavaMail的命名体现了其核心功能——在Java环境下处理邮件。

示例:javaProperties props=new Properties();props.put(mail.smtp.auth, true);props.put(mail.smtp.starttls.enable, true);props.put(mail.smtp.host, smtp.example.com);props.put(mail.smtp.port, 587);

Session session=Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }});

try { Message message=new MimeMessage(session); message.setFrom(new InternetAddress(username@example.com)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient@example.com)); message.setSubject(Test Email); message.setText(This is a test email.);

Transport.send(message); System.out.println(Email sent successfully.);} catch (MessagingException e) { throw new RuntimeException(Failed to send email., e);}

通过以上示例,您可以看到如何使用JavaMail发送一封简单的邮件。希望对您的学习或工作有所帮助。

这篇文章主要介绍了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><h1>hello!!spring image html mail</h1><a href=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文件:

JavaMail邮件发送原理及步骤详细解析?

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:p="www.springframework.org/schema/p" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xmlns:aop="www.springframework.org/schema/aop" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd www.springframework.org/schema/context 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=发件人邮箱全拼

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1502个文字,预计阅读时间需要7分钟。

JavaMail邮件发送原理及步骤详细解析?

本文主要介绍了JavaMail邮件发送机制,通过示例代码详细阐述了其使用方法。对于需要学习或工作的朋友,具有一定的参考价值。以下是对JavaMail的概念、命名意义和提供的示例的简要概述:

概念:JavaMail是一个Java API,用于发送和接收电子邮件。它提供了发送邮件、读取邮件、管理邮件服务器等功能。

命名意义:JavaMail的命名体现了其核心功能——在Java环境下处理邮件。

示例:javaProperties props=new Properties();props.put(mail.smtp.auth, true);props.put(mail.smtp.starttls.enable, true);props.put(mail.smtp.host, smtp.example.com);props.put(mail.smtp.port, 587);

Session session=Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }});

try { Message message=new MimeMessage(session); message.setFrom(new InternetAddress(username@example.com)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient@example.com)); message.setSubject(Test Email); message.setText(This is a test email.);

Transport.send(message); System.out.println(Email sent successfully.);} catch (MessagingException e) { throw new RuntimeException(Failed to send email., e);}

通过以上示例,您可以看到如何使用JavaMail发送一封简单的邮件。希望对您的学习或工作有所帮助。

这篇文章主要介绍了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><h1>hello!!spring image html mail</h1><a href=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文件:

JavaMail邮件发送原理及步骤详细解析?

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework.org/schema/beans" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns:p="www.springframework.org/schema/p" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xmlns:aop="www.springframework.org/schema/aop" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx.xsd www.springframework.org/schema/context 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=发件人邮箱全拼

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。