当前位置: 首页 > 编程笔记 >

JavaMail邮件发送机制详解

严兴言
2023-03-14
本文向大家介绍JavaMail邮件发送机制详解,包括了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=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=发件人邮箱全拼

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍基于java使用JavaMail发送邮件,包括了基于java使用JavaMail发送邮件的使用技巧和注意事项,需要的朋友参考一下 一、邮件的相关概念 邮件协议。主要包括: SMTP协议:Simple Mail Transfer Protocol,即简单邮件传输协议,用于发送电子邮件 POP3协议:Post Office Protocol 3,即邮局协议的第三个版本,用于接收邮件 IM

  • 本文向大家介绍JavaMail实现邮件发送的方法,包括了JavaMail实现邮件发送的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JavaMail实现邮件发送的方法。分享给大家供大家参考。具体如下: 下载 activation.jar 和 mail.jar 配置CLASSPATH,将上面两个jar包加入到CLASSPATH中。 JavaBean : "SendEmail.java"

  • 我阅读了javamail-android的指南和一些关于StackOverflow的帖子。例如。如何在Android中使用JavaMail API发送邮件?我收到消息“邮件发送成功...”。但邮件不会到达收件人地址。我有以下输出: 我如何修复它或使用另一种方式发送电子邮件?

  • 这是我发送邮件的代码 我在tomcat的lib文件夹中添加了和。我将eclipse与Tomcat7、JRE7和Struts2一起使用。通过Java发送邮件时出现以下控制台错误:

  • 本文向大家介绍JavaMail入门教程之发送邮件(3),包括了JavaMail入门教程之发送邮件(3)的使用技巧和注意事项,需要的朋友参考一下 JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送协议的底层实施细节,应用程序调用这个类中的方法就可以把Message对象中封装的邮件数据发送到指定的SMTP服务器。

  • 我想用Java发送一封邮件,但使用我在http://www.tutorialspoint.com/Java/java_sending_email.htm找到的这篇泰文教程是行不通的。 这里是我的主要方法 我有以下错误: javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25;嵌套异常为:java.net.connectException: