关于使用qq邮箱作为服务器发送邮件的问题

帅的不像实力派 发布于 2016/12/05 22:22
阅读 463
收藏 0

通过百度写了一个发送邮件的实例,使用的是javaMail.但是一直提示

A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28。

根据百度的提示,在邮箱设置中开通了POP服务,也获取了授权码,但还是不起作用。下面是写的代码

package com.etoak.test;


import java.util.Date;
import java.util.Properties;


import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class TestMail {


/**
* 发件人的邮箱和密码
* @param args
*/
private static String myEmailAccount = "此处为邮箱";
private static String myEmailPassword = "此处为授权码";
private static String myEmialHost = "smtp.qq.com";
private static String receiver = "此处为邮箱";
public static void main(String[] args) throws Exception {
//1.创建参数配置
Properties prop = new Properties();
prop.setProperty("mail.transport.protocol","smtp");
prop.setProperty("mail.host", myEmialHost);
prop.setProperty("mail.smtp.auth","true");
prop.setProperty("isSSL","true");
//2.根据配置创建会话,用于和邮件服务器交互
Session session = Session.getDefaultInstance(prop);
session.setDebug(true);
MimeMessage message = createMimeMessage(session,myEmailAccount,receiver);//创建一封邮件
Transport transport = session.getTransport();
transport.connect(myEmailAccount,myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}


/**
* 创建一份简单邮件
*/
public static MimeMessage createMimeMessage(Session session,String sendMail,String receiveMail) throws Exception{
MimeMessage  message = new MimeMessage (session);
message.setFrom(new InternetAddress(sendMail,"某宝王","UTF-8"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail,"XX用户","UTF-8"));
message.setSubject("年终大庆","UTF-8");
message.setContent("XX用户你好,今天全场五折起","text/html;charset=UTF-8");
message.setSentDate(new Date());;
message.saveChanges();
return message;
}

}


加载中
返回顶部
顶部