Java使用QQ邮箱或网易163邮箱发送自定义CSS样式邮件


前言

我们可能有时候项目中会有需求要给客户或者员工发送一些提醒之类的邮件,这时候我们就可以使用Java并且利用定时器完成该操作


一、战前准备

1.导入依赖

这里我们使用Maven项目,在pom文件中导入所需要的mail的jar包

<!--邮件-->
<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

2.配置文件

先来创建一个properties配置文件吧

在这里插入图片描述

#isMail=Yes
isMail=No

# 使用QQ邮箱发送使用该host
mail.host=smtp.qq.com
# 使用网易邮箱发送使用该host
#mail.host=smtp.163.com

# smtp协议
mail.transport.protocol=smtp

# 是否开启认证
mail.smtp.auth=true

# 定义发送者的邮箱
fromMail=xxx@163.com
fromQQMail=xxx@qq.com

3.邮箱工具类

先来使用网易163给别的邮箱发送邮件

package cn.liu783.vueappjava.util;

import org.springframework.core.io.support.PropertiesLoaderUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class MailUtils {

    public static void send163Mail(String address, String headline, String content) {

        Thread thread = new Thread(() -> {
            Properties properties = null;
            try {
            	// 读取配置文件
                properties = PropertiesLoaderUtils.loadAllProperties("properties/mail.properties");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            // 开始auth认证,需要你输入你密码
            Properties finalProperties = properties;
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                	// 获取发送者邮箱,并填写密码
                    return new PasswordAuthentication(finalProperties.getProperty("fromMail") , "你的密码");
                }
            });
//            Session session = Session.getDefaultInstance(properties);
			// 开启Debug日志
            session.setDebug(true);

            try {
                System.out.println("使用网易邮箱发送邮件");
                Transport transport = session.getTransport();
                transport.connect();

                MimeMessage mimeMessage = new MimeMessage(session);
                mimeMessage.setFrom(new InternetAddress(properties.getProperty("fromMail")));
                mimeMessage.setRecipients(Message.RecipientType.TO, address);
                String subjects = MimeUtility.encodeWord(headline, "UTF-8", "Q");
                mimeMessage.setSubject(subjects);
                mimeMessage.setContent(content, "text/html;charset=UTF-8");

                transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
                transport.close();

                System.out.println("使用网易邮箱发送邮件完成");
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        });
        thread.start();
    }

}

二、战争开始

1.QQ邮箱发送

方法复制一份,然后把 fromMail 都改为fromQQMail,然后记得注释掉配置文件中其他的mail.host

public static void main(String[] args) {
		// 设置要发送的文本,html格式,所以可以内嵌css
        String content = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"font-family:'黑体',Helvetica,Arial,sans-serif;font-size:14px;background: lightblue;border-radius: 10px;\" width=\"100%\"><tbody><tr>\n" +
                "\t\t<td style=\"font-family:Helvetica,Arial,sans-serif;font-size:14px;text-align: center;color: white;\">\n" +
                "\t\t<h3 style=\"margin: 15px 0px;\">页面访问升级中...</h3>\n" +
                "\t\t<a style=\"display: inline-block;text-decoration:none;width: 150px;height: 40px;background: steelblue;line-height: 40px;color: white;border-radius: 10px;margin-bottom: 20px;\" \n" +
                "\t\thref=\"xxx\">www.se783.co</a>\n" +
                "\t\t<hr style=\"color:steelblue;border: 2px solid aliceblue;margin: 5px 20px;border-radius: 10px;\">\n" +
                "\t\t<h5>温馨提示<br>海外网络永久域名<br>www.se783.co<br>记住防止迷路</h5>\n" +
                "\t\t<table width=\"100%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\"><tbody><tr>\n" +
                "\t\t<td style=\"text-align: right;\">\n" +
                "\t\t<p style=\"font-size:14px;line-height:24px;font-family: '黑体', sans-serif;padding:0px 10px;margin:8px\">尊敬的访问者:<b>FanX</b></p>\n" +
                "\t\t<p style=\"color:white;font-size:14px;line-height:24px;font-family:'黑体',Helvetica,Arial,sans-serif;padding:0px 10px;margin:8px\">您的访问次数程度:<span style=\"color:#af5c61;\">99颗星</span> (最高5颗星)</p>\n" +
                "\t\t</td></tr></tbody></table></td></tr></tbody></table>";
//        send163Mail("2217892328@qq.com","你好鸭🙂",c);
		// 你要发送给谁,下面填他的邮箱地址
        sendQQMail("xx@xx.com","你好鸭🙂",content);
    }

2.网易邮箱发送

网易的发送就直接调用刚刚工具类中的方法就可以了,我把我的模板发给大家一份,然后看下效果

<table border="0" cellspacing="0" cellpadding="0" style="font-family:'黑体',Helvetica,Arial,sans-serif;font-size:14px;background: lightblue;border-radius: 10px;"
		 width="100%">
	<tbody>
		<tr>
			<td style="font-family:Helvetica,Arial,sans-serif;font-size:14px;text-align: center;color: white;">
				<h3 style="margin: 15px 0px;">页面访问升级中...</h3>
				<a style="display: inline-block;text-decoration:none;width: 150px;height: 40px;background: steelblue;line-height: 40px;color: white;border-radius: 10px;margin-bottom: 20px;"
				 href="#">www.se783.co</a>
				<hr style="color:steelblue;border: 2px solid aliceblue;margin: 5px 20px;border-radius: 10px;">
				<h5>温馨提示<br>海外网络永久域名<br>www.se783.co<br>记住防止迷路</h5>
				<table width="100%" border="0" cellpadding="5" cellspacing="0">
					<tbody>
						<tr>
							<td style="text-align: right;">
								<p style="font-size:14px;line-height:24px;font-family: '黑体', sans-serif;padding:0px 10px;margin:8px">尊敬的访问者:<b>FanX</b></p>
								<p style="color:white;font-size:14px;line-height:24px;font-family:'黑体',Helvetica,Arial,sans-serif;padding:0px 10px;margin:8px">您的访问次数程度:<span
									 style="color:#af5c61;">99颗星</span> (最高5颗星)</p>
							</td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>

在这里插入图片描述
在这里插入图片描述


战后反思

好了,自己改改邮箱和内容去试试吧

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值