Java发送Email

作者: cooze | 来源:发表于2017-08-02 10:07 被阅读56次

我们在注册XXX网站账号成为会员的时候经常会通过邮箱等方式发送验证码或激活账号。本文将会介绍两种常用的Java发送Email的案例(以撸代码为主 0.0 )。

1、通过commons-email发送邮件
2、通过Spring的组件发送邮件(推荐)

下面首先介绍的是使用commons-email组件发送Email.

一、使用commons-email发送电子邮件

commons-email是Apache基金会开源的一款Email发送组件,commons-email是基于Java Mail之上的封装Email发送API。

commons-email提供如下Java Email发送API类。

1、SimpleEmail
SimpleEmail类通常用来发送普通文本类型的邮件。
2、MultiPartEmail
MultiPartEmail类通常用来发送流媒体类型的邮件,允许附件和文本类型数据一起发送。
3、HtmlEmail
HtmlEmail类通常用来发送html格式的邮件,他也支持邮件携带普通文本、附件和内嵌图片。
4、ImageHtmlEmail
ImageHtmlEmail类通常是用来发送Html格式并内嵌图片的邮件,它拥有所有HtmlEmail的功能,但是图片主要是以html内嵌的为主。

在使用commons-email之前首先引入他的依赖包到我们的工程中,commons-email依赖如下:

    <dependencies>
        <!--commons-email依赖包-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
          <!-- 指定JDK版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

这里主要介绍SimpleEmail和MultiPartEmail两种发邮件API的使用:

1、SimpleEmail发送普通文本的邮件

代码:

public static void main(String[] args) throws EmailException {
        Email email = new SimpleEmail();
        //设置邮箱服务器,在这里我使用的163邮箱
        email.setHostName("smtp.163.com");
        //填写邮件服务器端口:465和25选填
        email.setSmtpPort(25);
        //开启debug日志
        //email.setDebug(true);
        //设置用户名(邮箱)和授权码(授权码是用于登录第三方邮件客户端的专用密码)
        //邮箱开启授权只需要登陆邮件,在里边设置一下就行了.
        email.setAuthenticator(new DefaultAuthenticator("your_email@163.com", "your_auth_password"));
        //开启ssl连接
        email.setSSLOnConnect(true);
        //填写发送者的邮箱
        email.setFrom("your_email@163.com");
        //填写发送日期
        email.setSentDate(new Date());
        //填写邮件标题
        email.setSubject("TestMail");
        //邮件内容
        email.setMsg("你好这是测试邮件");

        //填写发送人,我用发给的是新浪邮箱,除了发送给QQ邮箱不行之外,你随意
        //注:发给QQ邮箱被退回了(腾讯邮箱服务对邮件检查比较严格导致)
        email.addTo("target_email_address@sina.com");
        //发送
        email.send();
    }

2、EmailAttachment和MultiPartEmail发送携带附件的邮件

代码:

public static void main(String[] args) throws EmailException {
        //创建附件
        EmailAttachment attachment = new EmailAttachment();
        //填写附件位置
        attachment.setPath("path_to_your_attachment/attachment.txt");
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        //填写附件描述
        attachment.setDescription("附件描述");
        //填写附件名称,要与上面一致填写路径中的附件名称一致
        //要不然收到附件的时候会有问题
        attachment.setName("attachment.txt");

        //创建邮件信息
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName("smtp.163.com");
        email.setSmtpPort(465);
        email.setDebug(true);
        //填写授权码和用户名
        email.setAuthenticator(new DefaultAuthenticator("your_email@163.com", "your_auth_password"));
        email.setSSLOnConnect(true);

        email.setFrom("your_email@163.com");
        email.addTo("target_email_address@sina.com");

        email.setSubject("附件");
        email.setMsg("我的附件发给你了.");

        // 添加附件
        email.attach(attachment);

        //发送邮件
        email.send();
    }

二、使用spring组件发送电子邮件

下面将介绍,如何使用Spring的提供的Email工具类发送电子邮件,还是以撸代码为主,简单、方便、粗暴,下面使用spring的email工具发送携带附件和内嵌网页,并且网页中内嵌图片电子邮件案例。

依赖包倒入:

    <dependencies>
        <!-- 倒入Spring支持邮件工具的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- java mail 依赖-->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.6</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 指定JDK版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

spring 发送email的代码示例:

public static void main(String[] args) throws MessagingException {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost("smtp.163.com");
        sender.setUsername("your_email_name@163.com");
        sender.setPassword("auth_password");//邮箱授权码

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setValidateAddresses(true);
        helper.setSentDate(new Date());
        helper.setSubject("网页内嵌图片");
        helper.setFrom("your_email_name@163.com");

        helper.setTo("target_email_name@qq.com");

        //  附件
        FileSystemResource file = new FileSystemResource(new File("path_to_attachment/attachment.txt"));
        helper.addAttachment("attachment.txt", file);

        // 文本信息中嵌入网页 这里填写的pictureId要和addInline中填的一致。
        helper.setText("<html><body>![](cid:pictureId)me</body></html>", true);

        // 网页内嵌图片
        FileSystemResource res = new FileSystemResource(new File("path_to_picture/picture.jpg"));
        helper.addInline("pictureId", res);
        sender.send(message);
    }

打完收工,谢谢!

相关文章

  • Java发送Email

    我们在注册XXX网站账号成为会员的时候经常会通过邮箱等方式发送验证码或激活账号。本文将会介绍两种常用的Java发送...

  • java发送Email

    引言 在开发中,有的时候会用到邮件平台通知。最常见的就是亚马逊经常给用户推荐一些商品,以及购买商品后的邮件通知。 ...

  • Java发送Email邮件问题

    SpringBoot项目整合JavaMail 采用smtp协议发送邮箱 邮箱采用的是QQ邮箱在windows环境...

  • 菜鸟笔记(二) - Java邮件发送实践

    本文将介绍多种使用Java发送Email的应用实践。 前提摘要:在学习Java Web的构建中,无论是触发型的邮件...

  • 2018-07-02

    发送邮件 //发送邮件 @ResponseBody @RequestMapping("email") public...

  • vapor 发送email

    导入库 邮件发送

  • email 发送邮件

  • 接口文档

    发送密码重置邮件 /api/authority/email?url=baidu.com&email=8779986...

  • 二、账号和个人页面的实现

    一、 个人注册功能实现 java8 Map的遍历 并保存文件 1.2 发送邮件过程中生成key并且绑定email,...

  • Java后端+PhantomJS +Echars生成数据图表

    这次遇到的问题要在发送email时,生成好看的数据图表,所以初步思路就是:java服务端+echars。由于ech...

网友评论

    本文标题:Java发送Email

    本文链接:https://www.haomeiwen.com/subject/sdmslxtx.html