javaMail发送邮件时出现错误怎么办(乐字节java架构,乐字节大数据)

Java Mail 发送邮件

发送普通文本的邮件

package com.xxxx.mail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

/**
 * 发送普通文本的邮件
 */
public class MailTest01 {
   

    public static void main(String[] args) throws Exception {
   

        // 定义邮箱服务器配置
        Properties prop = new Properties();
        // 设置邮件服务器主机名 (163 邮件服务器地址:"mail.smtp.host"  "smtp.163.com")
        prop.setProperty("mail.smtp.host", "smtp.163.com");
        // 设置邮件服务器的端口
        prop.setProperty("mail.smtp.port", "25");
        // 设置邮件服务器认证属性 (设置为true表示发送服务器需要身份验证)
        prop.setProperty("mail.smtp.auth", "true");
        // 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证
        // prop.setProperty("mail.smtp.port", "465");
        // prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        // prop.setProperty("mail.smtp.socketFactory.port", "465");

        // 使用JavaMail发送邮件的5个步骤
        // 1. 创建session
        Session session = Session.getInstance(prop);
        // 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        // 2. 通过session得到transport对象
        Transport ts = session.getTransport();
        // 3. 使用邮箱的用户名和密码连上邮件服务器(用户名只需写@前面的即可,密码是指授权码)
        ts.connect("smtp.163.com", "用户名", "密码");
        // 4. 创建邮件
        // 发送普通文本的邮件
        Message message = createSimpleTxtMail(session);
        
        // 5. 发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        // 关闭transport对象
        ts.close();

    }

    /**
     * 普通文本邮件
     *      创建一封只包含文本的邮件
     * @param session
     * @return
     */
    public static MimeMessage createSimpleTxtMail(Session session) throws MessagingException {
   
        // 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 设置邮件的发件人的邮箱地址
        message.setFrom(new InternetAddress("发件人的邮箱地址"));
        // 设置邮件的收件人的邮箱地址 (现在发件人和收件人是一样的,那就是自己给自己发)
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人的邮箱地址"));

        // 发送给多个收件人
        // message.setRecipients(Message.RecipientType.TO, new InternetAddress[] {});
        // Cc: 抄送(可选)
        // message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(""));
        // Bcc: 密送(可选)
        // message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(""));

        // 邮件的主题
        message.setSubject("测试文本邮件");
        // 设置发送日期
        message.setSentDate(new Date());
        // 邮件的文本内容 (setText():纯文本内容)
        message.setText("你好,这是一封测试邮件!");

        // 返回创建好的邮件对象
        return message;
    }
}

效果如下:

在这里插入图片描述

发送HTML内容的邮件

package com.xxxx.mail;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

/**
 * 发送HTML内容的邮件
 */
public class MailTest02 {
   

    public static void main(String[] args) throws Exception {
   

        // 定义邮箱服务器配置
        Properties prop = new Properties();
        // 设置邮件服务器主机名 (163 邮件服务器地址:"mail.smtp.host"  "smtp.163.com")
        prop.setProperty("mail.smtp.host", "smtp.163.com");
        // 设置邮件服务器的端口
        prop.setProperty("mail.smtp.port", "25");
        // 设置邮件服务器认证属性 (设置为true表示发送服务器需要身份验证)
        prop.setProperty("mail.smtp.auth", "true");

        // 使用JavaMail发送邮件的5个步骤
        // 1. 创建session
        Session session = Session.getInstance(prop
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值