通过 JavaMail 实现QQ邮件发送

2024-10-14 00:07:35

1、先开启你所要用的QQ服务;方法:登陆你的qq邮箱

通过 JavaMail 实现QQ邮件发送

3、发送短信 —–点击确定

通过 JavaMail 实现QQ邮件发送

4、稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到16位的字母,如:tccclqwjbkaadogg得到的授权码一定要保存好,程序中要使用

5、完成上面准备步骤后:可以使用下面的代码import java.util.Date;import java.util.Properties;import javax.mail.Bo颊俄岿髭dyPart;import javax.mail.Message;import javax.mail.Multipart;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;public class JavaMailSend { public static void main(String[] args) { sendMail("222666666@qq.com","南无阿弥陀佛,南无地藏王菩萨","佛菩萨来邮件了(⊙o⊙)哦,赶紧去看看吧~"); } public static boolean sendMail(String emailTo,String content,String subject) { String fromEmail="2366688888@qq.com";//你的QQ邮箱 String eMailType="smtp.qq.com"; String eMailAuthPassword="gnqtbmbptxonabcd";//QQ邮箱授权码 开通POP3/SMTP服务 的授权码 String body = content; //正文内容 try {//****************************创建会话************************************ Properties props = new Properties(); props.put("mail.smtp.host",eMailType);//发件人使用发邮件的电子信箱服务器 props.put("mail.password",eMailAuthPassword); props.put("mail.transport.protocol", "smtp"); props.setProperty("mail.debug", "true"); props.put("mail.smtp.auth","true"); //这样才能通过验证 //下面这三句很重要,如果没有加入进去,qq邮箱会验证不成功,一直报503错误 props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.port", "465"); //获得默认的session对象 Session mailSession = Session.getInstance(props); mailSession.setDebug(true);//*****************************构造消息*************************************** MimeMessage mimeMessage = new MimeMessage(mailSession); InternetAddress from=new InternetAddress(fromEmail); mimeMessage.setFrom(from); InternetAddress to=new InternetAddress(emailTo); //设置收件人地址并规定其类型 mimeMessage.setRecipient(Message.RecipientType.TO,to); mimeMessage.setSentDate(new Date()); //设置发信时间 mimeMessage.setSubject(subject); //设置主题 mimeMessage.setText(body); //设置 正文 //给消息对象设置内容 BodyPart bodyPart=new MimeBodyPart(); //新建一个存放信件内容的BodyPart对象 bodyPart.setContent(body, "text/html;charset= GB2312"); //设置 发送邮件内容为HTML类型,并为中文编码 Multipart multipart=new MimeMultipart(); multipart.addBodyPart(bodyPart); mimeMessage.setContent(multipart); mimeMessage.saveChanges(); //发送消息 Transport transport=mailSession.getTransport("smtp"); transport.connect(eMailType,fromEmail,eMailAuthPassword); //发邮件人帐户和开通POP3/IMAP的“授权码” transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); transport.close();//*******************************发送消息******************************** mimeMessage.writeTo(System.out);//保存消息 并在控制台 显示消息对象中属性信息 System.out.println("邮件已成功发送到 " + emailTo); return true; } catch (Exception e) { e.printStackTrace(); System.out.println(e); return false; } }}

猜你喜欢