java简单邮件发送(纯文本)

java简单邮件发送

邮件发送

发送邮件:SMTP协议

接收邮件:POP3协议

收件人

主题

内容正文

邮件服务器相当于现实中的邮局

(1)jar包

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ar3jg0RE-1620034320836)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210503154553716.png)]

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>

(2)简单邮件

纯文本

package com;

import com.sun.mail.util.MailSSLSocketFactory;

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

public class SimpleMail {
    //发送简单邮件
    public static void main(String[] args) throws GeneralSecurityException, MessagingException {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "smtp.qq.com");//设置QQ邮箱服务器
        prop.setProperty("mail.transport.protocol", "smtp");//邮件发送协议
        prop.setProperty("mail.smtp.auth", "true");//需要验证用户名,密码

        //QQ邮箱还要设置SSL加密
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);

        //发送邮件的五个步骤
        //1 创建session对象
        //QQ特有
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("1677501301@qq.com", "授权码");
            }
        });
        //开启session的debug模式
        session.setDebug(true);
        //2 通过session得到transport对象
        Transport ts = session.getTransport();

        //3 使用邮件的用户名和授权码连上邮件服务器
        ts.connect("smtp.qq.com", "1677501301@qq.com", "授权码");

        //4 创建邮件,写信
        //传递session
        MimeMessage message = new MimeMessage(session);
        //发件人
        message.setFrom(new InternetAddress("1677501301@qq.com"));
        //收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("1677501301@qq.com"));
        //主题
        message.setSubject("文本邮件2");
        //正文
        message.setContent("<h1 style=\"color: aqua\">写邮件2</h1>", "text/html;charset=UTF-8");

        //发送邮件
        ts.sendMessage(message, message.getAllRecipients());

        //关闭连接
        ts.close();

    }

}

成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XuIAzEQU-1620034320849)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210503172551627.png)]

开启debug

DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL true
220 newxmesmtplogicsvrszc9.qq.com XMail Esmtp QQ Mail Server.
DEBUG SMTP: connected to host "smtp.qq.com", port: 465

EHLO DESKTOP-56DNUG7
250-newxmesmtplogicsvrszc9.qq.com
250-PIPELINING
250-SIZE 73400320
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
250-AUTH=LOGIN
250-MAILCOMPRESS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<1677501301@qq.com>
250 OK.
RCPT TO:<1677501301@qq.com>
250 OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   1677501301@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>.
From: 1677501301@qq.com
To: 1677501301@qq.com
Message-ID: <1635985705.0.1620034096085.JavaMail.asus@smtp.qq.com>
Subject: =?UTF-8?B?5paH5pys6YKu5Lu2Mg==?=
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<h1 style=3D"color: aqua">=E5=91=A8=E8=8B=A5=E6=95=8F=E5=86=99=E9=82=AE=E4=
=BB=B62</h1>
.
250 OK: queued as.
QUIT
221 Bye.

Process finished with exit code 0

(3)复杂文件

有附件和图片

(4)开启服务

发送邮件需要获得协议和支持,开启服务POP3/SMPT服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值