JAVA学习---QQ发送邮件与接收
QQ邮箱的设置
PS:菜鸟一个,只是为了防止犯下同一个错误,所以,写了这个博客,欢迎大家多提意见,虽然我不一定会听,但我一定认真考虑。
第一步:QQ邮箱的设置
话不多说,直接配置:
登录QQ邮箱后,进入设置页面,
在最上面一行,
账户菜单下,最下边有四个服务,开启pop/smtp或imap/smtp这两个,开启这两个就可以了。
第二步:开始上代码
发送邮件代码
package test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class test1{
public static void main(String [] args)
{
// 收件人电子邮箱
String to = "xxxxxxxxx@qq.com";
// 发件人电子邮箱
String from = "xxxxxxx@foxmail.com";
// 指定发送邮件的主机为 localhost
String host = "smtp.qq.com";
// 获取系统属性
Properties props = System.getProperties();
// 设置邮件服务器
props.setProperty("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", from);
props.setProperty("mail.password", "邮箱密码");
props.setProperty("mail.smtp.port", "587"); //设置成465不可以发送,设置成587可以
// 获取默认session对象
/**
*这部分设置很正要,很多邮箱都需要这个验证
**/
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"qtxdata@foxmail.com", "zpyqxakezptyfcdh");// Specify the Username and the PassWord
}
});
session.setDebug(true);
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
/**
*TO是发送
*BCC是密送
*CC是抄送
**/
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("这是一封邮件正文带图片"
+"<img src='http://editerupload.eepw.com.cn/201809/61001537857032.jpg'>的邮件");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
收取邮件不完整代码
PS:没有解析部分
package test;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
public class test2{
{
String pop3Server = "pop.qq.com";
String protocol = "pop3";
String username = "qtxdata@foxmail.com";
String password = "zpyqxakezptyfcdh";
Properties props = new Properties();
props.setProperty("mail.transport.protocol", protocol); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.pop3.host", pop3Server);
props.setProperty("mail.pop3.ssl.enable", "true");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.auth", "true");
Session session = Session.getDefaultInstance(props,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"qtxdata@foxmail.com", "zpyqxakezptyfcdh");
}
});
session.setDebug(false);
Store store=null;
try {
store = session.getStore(protocol);
store.connect(pop3Server, username, password);
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
Folder folder =null;
try {
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
for (Message message : messages) {
String subject = message.getSubject();// 获得邮件主题
Address from = (Address) message.getFrom()[0];// 获得发送者地址
System.out.println("邮件的主题为: " + subject + "\t发件人地址为: " + from);
System.out.println("邮件的内容为:");
message.writeTo(System.out);// 输出邮件内容到控制台
}
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(folder!=null) {
folder.close(false);
}
if(store!=null) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
自动被识别为垃圾邮箱
这个问题不是很严重,前台有提示就成。
谢谢观看,如果能提出合理的意见就更好了。