email

Email 邮件发送

0、导入 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhiyou100</groupId>
    <artifactId>email</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- 强制排出文件  -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

    <!--  控制 版本    -->
    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!--  编译jdk版本  -->
        <jdk.version>1.8</jdk.version>

        <!--  依赖版本  -->
        <!--  spring 版本      -->
        <spring.version>4.1.2.RELEASE</spring.version>
        <!--  sql 版本      -->
        <mysql.version>8.0.20</mysql.version>
        <poi.version>3.17</poi.version>
        <xerces.version>2.11.0</xerces.version>
    </properties>

    <!-- 导入依赖   -->
    <dependencies>
        <!-- spring start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!-- spring end -->

        <!-- 其他开始-->
        <dependency>
            <!--  lombok 依赖 偷懒神器 @Data 自动生成 get set toString 无参构造方法 equals hashCode -->
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <!--  c3po 依赖              -->
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>


        <!--  hutool 工具类      -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.15</version>
        </dependency>


        <dependency>
            <!-- fastJson  -->
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>


        <dependency>
            <!--  MySQL  驱动         -->
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

        <dependency>
            <!-- dom 解析           -->
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- web 开始  -->
        <!--        <dependency>-->
        <!--            <groupId>javax</groupId>-->
        <!--            <artifactId>javaee-api</artifactId>-->
        <!--            <version>6.0</version>-->
        <!--            <scope>provided</scope>-->
        <!--        </dependency>-->

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.websocket</groupId>
            <artifactId>javax.websocket-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- web 结束  -->


        <dependency>
            <!--  cglib  依赖        -->
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>


        <!--   mybatis 依赖      -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!-- 分页 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
        </dependency>


        <!-- 测试     -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <!--  commons-io         -->
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>


        <dependency>
            <!--  文件上传  -->
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>${xerces.version}</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>


        <dependency>
            <!-- oracle 依赖           -->
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>


        <!-- 邮件依赖开始       -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
        <!-- 邮件依赖结束      -->


        <!-- 其他结束-->
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

一、注册一个 网易 162 邮箱

162

注册

  • 注册成功

007ydorvly1gisjf1u6iej32lc1mcqv5

二、开启第三方授权,

  • 点击 设置 -> POP3/SMTP/IMAP 设置

fafdafadfasSQIVHs

  • 点击 开启

    注意需要发送短信,会有一个授权码,记一下

开启

三、创建程序,发送邮件

标准端口25,也可以使用加密端口465或587

  • Test01SendMail.java
package com.zhiyou100.email;

import com.sun.xml.internal.xsom.impl.parser.SubstGroupBaseTypeRef;
import lombok.SneakyThrows;
import org.springframework.web.bind.ServletRequestDataBinder;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * 发送邮件
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-09-16 10:28
 */
public class Test01SendMail {
    /**
     * 邮箱名字
     */
    public static final String MY_EMAIL_ACCOUNT = "javayanglei2001@126.com";
    /**
     * 授权码
     */
    public static final String MY_EMAIL_PASSWORD = "OAJFKDISCBDVRGJE";
    /**
     * SMTP服务器: smtp.126.com
     */
    public static final String EMAIL_163_SMTP_HOST = "smtp.126.com";
    /**
     * 端口号
     * QQ邮箱:SMTP服务器是 smtp.qq.com,端口是465/587;
     * 163邮箱:SMTP服务器是 smtp.163.com,端口是 25;
     * Gmail邮箱:SMTP服务器是 smtp.gmail.com,端口是465/587。
     */
    public static final String EMAIL_163_SMTP_PORT = "25";

    @SneakyThrows
    public static void main(String[] args) {
        // 收件人
        String receiver = "yanglei20011123@163.com";
        // 标题
        String subject = "你好,我是小号";
        // 内容
        String content = "<h1>发送成功!!!</h1>";
        sendMessage(receiver,subject,content);
    }

    public static void sendMessage(String receiver,String subject,String content)throws Exception {
        //属性集
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", EMAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", EMAIL_163_SMTP_PORT);
        p.setProperty("mail.smtp.socketFactory.port", EMAIL_163_SMTP_PORT);
        //是否需要认证
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");
        //获取连接
        Session session = Session.getInstance(p, new Authenticator() {
            // 设置认证账户信息
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        //设置debug测试
        session.setDebug(true);
        System.out.println("创建邮件");
        MimeMessage message = new MimeMessage(session);
        // 发件人
        message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
        // 收件人和抄送人
        message.setRecipients(Message.RecipientType.TO, receiver);
        // 内容(这个内容还不能乱写,有可能会被SMTP拒绝掉;多试几次吧)
        message.setSubject(subject);
        message.setContent(content, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        message.saveChanges();//保存设置
        System.out.println("准备发送");
        Transport.send(message);
        System.out.println("发送成功!");
    }
}
  • 发送成功!
    image-20200916110227142
posted @ 2020-09-16 15:36  javaYanglei  阅读(530)  评论(0编辑  收藏  举报