好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring+maven实现发送邮件功能

前言

java编程中发邮件也是常用的。但是原生的jdk自带的 发送邮件 用起来还是比较麻烦的。 spring 框架在java语言中完全是神一样的存在,通过spring框架的邮件工具来发送邮件就非常方便了,本文就主要讲解了java编程中利用spring提供的邮件工具来发送邮件。

编码实现发送邮件

1、首先我们需要一个spring框架的环境
2、发送邮件需要的核心依赖包:spring-context-supportxxx.jar、activation-1.1.jar、javax.mail-1.5.2.jar
3、导入以上核心包到项目中

maven 依赖配置如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<!-- mail and spring-context-support for send email -->

<dependency>

  <groupid>org.springframework</groupid>

  <artifactid>spring-context-support</artifactid>

  <version>${spring.version}</version>

</dependency>

 

<dependency>

  <groupid>com.sun.mail</groupid>

  <artifactid>javax.mail</artifactid>

  <version> 1.5 . 2 </version>

</dependency>

<!-- /email -->

4、导入了上面的依赖我们就开始编写一个简单的email发送sample

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

package service;

 

import java.io.file;

import javax.mail.internet.mimemessage;

import javax.mail.internet.mimeutility;

import org.apache.commons.logging.log;

import org.apache.commons.logging.logfactory;

import org.springframework.core.io.filesystemresource;

import org.springframework.mail.javamail.javamailsenderimpl;

import org.springframework.mail.javamail.mimemessagehelper;

 

public class emailsample {

  /** 日志 **/

  private static final log log = logfactory.getlog(emailsample. class );

 

  /**

   * 邮件测试工具类

   *

   * @param subject

   *   邮件主题

   * @param content

   *   html格式的邮件内容

   */

  public static void sendfilemail(string subject, string content) {

   javamailsenderimpl senderimpl = new javamailsenderimpl();

   // 设置自己登陆email的服务商提供的host

   senderimpl.sethost( "smtp.126.com" );

   // 设置自己登陆邮箱账号

   senderimpl.setusername( "test@126.com" );

   // 邮箱密码

   senderimpl.setpassword( "******" );

   try {

    // 建立html邮件消息

    mimemessage mailmessage = senderimpl.createmimemessage();

    // true表示开始附件模式.如果邮件不需要附件设置成false即可

    mimemessagehelper messagehelper = new mimemessagehelper(

      mailmessage, true , "utf-8" );

    // 设置收信人的email地址

    messagehelper.setto( "111@qq.com" );

    // 设置寄信人的email地址{与上面登陆的邮件一致}

    messagehelper.setfrom( "test@126.com" );

    // 设置邮件发送内容的主题

    messagehelper.setsubject(subject);

    // true 表示启动html格式的邮件

    messagehelper.settext( "<html><title>这是一封邮件</title><body>"

      + content + "</body></html>" , true );

    // 如不需要附件,这里可以省略---------------------------------------start

    // 读取附件一

    filesystemresource file1 = new filesystemresource( new file(

      "e:/test.jpg" ));

    // 读取附件二

    filesystemresource file2 = new filesystemresource( new file(

      "e:/测试.txt" ));

    // 添加附件一

    messagehelper.addattachment( "test.jpg" , file1);

    // 添加附件二

    // 附件名有中文可能出现乱码

    messagehelper

      .addattachment(mimeutility.encodeword( "测试.txt" ), file2);

    // 如不需要附件,这里可以省略------------------------------------------end

    // 发送邮件

    senderimpl.send(mailmessage);

    log.info( "email send success!" );

   } catch (exception e) {

    log.error( "email send error!" + e.getmessage());

   }

 

  }

 

  public static void main(string[] agrs) {

   // 注意测试需要修改您自己的邮件服务商host,登陆邮件用户,邮件密码,附件,收信人地址

   sendfilemail( "测试邮件" , "<h1>测试邮件标题</h1>" );

  }

}

【注意事项】:运行main方法测试时候修改参数:服务商host、登陆用户、登陆密码、收信人地址、附件(不用附件可省略)

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

原文链接:https://blog.csdn.net/uniquewonderq/article/details/80002007

查看更多关于spring+maven实现发送邮件功能的详细内容...

  阅读:62次