Java发送Email

我们在注册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);
    }

打完收工,谢谢!

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容