java模拟登录qq邮箱_使用Java实现qq邮箱发送邮件

本文实例为大家分享了Java操作qq邮箱发送邮件的具体代码,供大家参考,具体内容如下

今天尝试了使用QQ邮箱的POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务来进行发送邮件!(这些个服务就是些协议,只有开启了之后就可以做一些操作)

步骤

1、登录QQ邮箱> 设置 > 账户

d7440da68f1ce65b676c9d25eb2afae3.png

2、找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务

开启 POP3/SMTP 服务 > 拿到授权码

0e05b9b68e3eaf95a84b0370eaee8d42.png

3、创建maven项目

4、在pom.xml导入依赖包

javax.mail

mail

1.4.7

5、创建java类 类名取为:SendEmailManger(注意包别导错了)

package com.xdl.util;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.Properties;

/**

* 邮件发送

* QQ邮箱--->别的邮箱

* @author shiyunpeng

*/

public class SendEmailManger extends Thread {

private String mailAdr;//邮箱

private String content;//邮件的内容

private String subject;//邮件的题目

public SendEmailManger(String mailAdr, String subject, String content) {

super();

this.mailAdr = mailAdr;

this.subject = subject;

this.content = content;

}

@Override

public void run() {

super.run();

try {

sendMail(mailAdr, subject, content);

} catch (Exception e) {

e.printStackTrace();

}

}

private void sendMail(String mailAdr, String subject, String content) throws Exception {

//加密的邮件套接字协议工厂

MailSSLSocketFactory sf = new MailSSLSocketFactory();

sf.setTrustAllHosts(true);

final Properties props = new Properties();

// 表示SMTP发送邮件,需要进行身份验证

props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.host", "smtp.qq.com");

// smtp登陆的账号、密码 ;需开启smtp登陆

props.setProperty("mail.debug", "true");

props.put("mail.user", "发送者邮箱");

props.put("mail.password", "授权码");

// 特别需要注意,要将ssl协议设置为true,否则会报530错误

props.put("mail.smtp.ssl.enable", "true");

props.put("mail.smtp.ssl.socketFactory", sf);

Authenticator authenticator = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

// 用户名、密码

String userName = props.getProperty("mail.user");

String password = props.getProperty("mail.password");

return new PasswordAuthentication(userName, password);

}

};

// 使用环境属性和授权信息,创建邮件会话

Session mailSession = Session.getInstance(props, authenticator);

// 创建邮件消息

MimeMessage message = new MimeMessage(mailSession);

// 设置发件人

try {

InternetAddress form = new InternetAddress(props.getProperty("mail.user"));

message.setFrom(form);

// 设置收件人

InternetAddress to = new InternetAddress(mailAdr);

message.setRecipient(Message.RecipientType.TO, to);

// 设置抄送

// InternetAddress cc = new InternetAddress("591566764@qq.com");

// message.setRecipient(RecipientType.CC, cc);

// 设置密送,其他的收件人不能看到密送的邮件地址

// InternetAddress bcc = new InternetAddress("mashen@163.com");

// message.setRecipient(RecipientType.CC, bcc);

// 设置邮件标题

message.setSubject(subject);

// 设置邮件的内容体

message.setContent(content, "text/html;charset=UTF-8");

// 发送邮件

Transport.send(message);

} catch (MessagingException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

SendEmailManger d = new SendEmailManger("接收邮件的邮箱", "syp:", "我呵呵,啊打:
加油哦!!!!....");

d.start();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值