Java发送QQ邮件附mail.jar和activation.jar包

111111是QQ邮箱账户,6666ufrtmxibjgf是QQ邮箱授权码,以下代码保存为hello.java

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

public class hello {
    public static void main(String[] args) {
        String to = "111111qq.com";
        String from = "111111@qq.com";
        String host = "smtp.qq.com";
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host",host);
        properties.put("mail.smtp.auth","true");


        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication()
            {
          //账户,邮箱授权码 return new PasswordAuthentication("111111","6666ufrtmxibjgf"); } }); try { MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); mimeMessage.setSubject("主题:黑客笔记"); mimeMessage.setText("正文:我是ailx10,我喜欢2进制安全和渗透技术"); Transport.send(mimeMessage); System.out.println("发送邮件成功"); } catch (MessagingException e) { e.printStackTrace(); } } }

运行:

下载两个jar包下载链接

(base) ➜  java javac -cp mail.jar:activation.jar hello.java 
(base) ➜  java java -cp .:mail.jar:activation.jar hello     
发送邮件成功

参考:
上述代码发送的是文本,发HTML和带附件参考:https://zhuanlan.zhihu.com/p/91153215https://blog.csdn.net/sdrfghb/article/details/126845550

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值