javaMail实现邮件的推送 javaMail不解释了,需要的自己去百度 直接说实现的步骤 1.设置自己的邮箱为发件邮箱,我这里是qq邮箱 好了,保存好你的授权码,很重要的 做到这里你已经完成了
javaMail实现邮件的推送
javaMail不解释了,需要的自己去百度
直接说实现的步骤
1.设置自己的邮箱为发件邮箱,我这里是qq邮箱
好了,保存好你的授权码,很重要的
做到这里你已经完成了很大的工作,现在就上代码:
//设置基本参数Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
Transport ts = session.getTransport();
ts.connect("smtp.qq.com", "你的qq邮箱", "生成的授权码");
Message message = createEmail(session,newsobj);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
好了,基本的设置完成,现在开始发邮件了
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("test@qq.com"));
//获取邮箱
//自己实现的是从数据库中获取用户的邮箱,遍历
Map<Integer, String> selectEmail = selectEmail();
Object[] arr = selectEmail.values().toArray();
InternetAddress[] sendTo = new InternetAddress[arr.length];
for (int i = 0; i < arr.length; i++) {
System.out.println("发送到:" + arr[i]);
sendTo[i] = new InternetAddress((String) arr[i]);
}
//设置收信人
// mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, sendTo);
message.setRecipients(Message.RecipientType.TO, sendTo);
// message.addRecipients(Message.RecipientType.TO, sendTo);
//设置邮件内容 数据库获取
List select = select(newsobj);
message.setSubject(select.get(1).toString());
String info = select.get(0).toString();
message.setContent(info, "text/html;charset=UTF-8");
message.saveChanges();
return message;
到这里已经完了,可能觉得不够详细,没事,我贴上完整的代码:
package com.email;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.entity.News;
import com.util.JDBCUtil;
public class EmailUtil {
public void email(News newsobj) {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(prop);
session.setDebug(true);
try {
Transport ts = session.getTransport();
ts.connect("smtp.qq.com", "test@qq.com", "ececbpqvhidseaha");
Message message = createEmail(session,newsobj);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Message createEmail(Session session,News newsobj) throws Exception{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("test@qq.com"));
//获取邮箱
Map<Integer, String> selectEmail = selectEmail();
/*
* for (Integer in : selectEmail.keySet()) { //map.keySet()返回的是所有key的值 String
* str = selectEmail.get(in);//得到每个key多对用value的值 // arr[in]=str;
*
*
* }
*/
Object[] arr = selectEmail.values().toArray();
InternetAddress[] sendTo = new InternetAddress[arr.length];
for (int i = 0; i < arr.length; i++) {
System.out.println("发送到:" + arr[i]);
sendTo[i] = new InternetAddress((String) arr[i]);
}
//设置收信人
// mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO, sendTo);
message.setRecipients(Message.RecipientType.TO, sendTo);
// message.addRecipients(Message.RecipientType.TO, sendTo);
//设置邮件内容 数据库获取
List select = select(newsobj);
message.setSubject(select.get(1).toString());
String info = select.get(0).toString();
message.setContent(info, "text/html;charset=UTF-8");
message.saveChanges();
return message;
}
//邮箱查询
public Map<Integer, String> selectEmail() {
//链接数据库查询所有用户的邮箱
//原生sql查询
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Map<Integer, String> map = new HashMap<Integer, String>();
try {
//1. 注册驱动
conn = JDBCUtil.getConn();
//3. 创建statement , 跟数据库打交道,一定需要这个对象
st = conn.createStatement();
//4. 执行查询 , 得到结果集
//查询用户列表下权限为1的普通用户的邮箱
String sql = "select email from user where role = 1;";
rs = st.executeQuery(sql);
//遍历
for (int i =0;rs.next();i++) {
String string = rs.getString("email");
map.put(i, string);
System.out.println(string);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtil.release(conn, st, rs);
}
return map;
}
//查询邮件内容
public List select(News newsobj) {
//链接数据库查询所有用户的邮箱
//原生sql查询
Connection conn = null;
Statement st = null;
ResultSet rs = null;
List list = new ArrayList();
try {
//1. 注册驱动
conn = JDBCUtil.getConn();
//3. 创建statement , 跟数据库打交道,一定需要这个对象
st = conn.createStatement();
//4. 执行查询 , 得到结果集
//查询用户列表下权限为1的普通用户的邮箱
// order by addtime desc limit 0,1 ;
String sql = "select title,content from news order by addtime desc limit 0,1;";
rs = st.executeQuery(sql);
while(rs.next()){
String string = rs.getString("Content");
list.add(0,rs.getString("Content"));
list.add(1,rs.getString("title"));
}
}
catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtil.release(conn, st, rs);
}
return list;
}
}