//服务器端
这个是服务端:注意账号和密码和虚拟机得写自己的!!
还有对应的qq邮箱和qq授权码
package 邮件管理;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.*;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
public class consumer {
public static void main(String[] args) throws Exception {
Verification v1=new Verification();
//创建连接工厂
ConnectionFactory fact=new ConnectionFactory();
//设置参数
fact.setHost("127.0.0.1"); //设置ip
fact.setPort(5672); //设置端口
fact.setVirtualHost("server"); //设置对应的虚拟机
fact.setUsername("admin");//用户名
fact.setPassword("admin"); //密码
//这个账号密码和虚拟机得自己写
Connection cont = fact.newConnection(); //创建连接Connection
Channel channel = cont.createChannel(); //创建Channel
Consumer consurme=new DefaultConsumer(channel){
//回调方法 ,当收到消息后会自动执行该方法
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//消息的标识 获取一些<交换机信息,路由器Key信息> 配置信息 最后的数据
try {
JSONObject js=new JSONObject().parseObject(new String(body));
String id = js.getString("id");
System.out.println("邮箱为:"+id+"发送成功");
v1.send(id);
System.out.println("当前暂停中");
Thread.sleep(10000); //下一次发送等待10秒
} catch (Exception e) {
e.printStackTrace();
}
}
};
channel.basicConsume("sendmail",true,consurme); //获取
}
}
class Verification{
private static String sender="xxxxxxxxxx@qq.com";//寄件人(写自己qq号)
private static String Authorization="iiljcvxpderebcci"; //qq授权码(自己qq的)
private static String myEmailSMTPHost = "smtp.qq.com";//qq的服务器
private static Session s1;
public String read(){ //生成随机码
Random r = new Random();
StringBuilder rs = new StringBuilder();
for (int i = 0; i < 7; i++) { //7位随机数
rs.append(r.nextInt(10));
}
return String.valueOf(rs);
}
static {
Properties ps = new Properties();
ps.setProperty("mail.transport.protocol", "smtp");
ps.setProperty("mail.smtp.host", myEmailSMTPHost);
ps.setProperty("mail.smtp.auth", "true");
s1=Session.getInstance(ps);
s1.setDebug(true); //可查看bug问题
}
public void send(String recipient) throws Exception { //发送邮件
MimeMessage msg=mailSending(s1,recipient);
Transport tport= s1.getTransport();
tport.connect(sender,Authorization); //qq号和密码
tport.sendMessage(msg, msg.getAllRecipients()); //发送
tport.close();
}
public MimeMessage mailSending(Session s1,String recipient) throws Exception{ // 邮件发送
MimeMessage msg=new MimeMessage(s1);
msg.setFrom(new InternetAddress(sender,"验证码","UTF-8")); //发件人
msg.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(recipient,"验证码接收用户","UTF-8"));
msg.setSubject("服务验证码");
msg.setContent("验证码为:"+read(),"text/html;charset=UTF-8"); //发送的邮件
Date date = new Date();
msg.setSentDate(date);
msg.saveChanges();//保存设置
return msg;
}
}
这个是客户端:注意账号和密码和虚拟机得写自己的!!
//客户端
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import util.Result;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;
public class fasinfuwuqi {
public static void main(String[] args) throws IOException, TimeoutException {
Producerq p1=new Producerq();
Scanner sc = new Scanner(System.in);
ConnectionFactory fact=new ConnectionFactory();
fact.setHost("127.0.0.1"); //设置ip
fact.setPort(5672); //设置端口
fact.setVirtualHost("server"); //设置对应的虚拟机
fact.setUsername("admin");//用户名
fact.setPassword("admin"); //密码
//自己的账号密码和虚拟机(如果要抄)
//创建连接Connection
Connection cont = fact.newConnection();
//创建Channel
Channel channel = cont.createChannel();
//创建队列
channel.queueDeclare("sendmail",true,false,false,null); //见图
String expr = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$";
while (true){
System.out.println("请输入认证qq邮箱号");
String next = sc.next();
try {
if(next.matches(expr)){
String convert = JsonZH.convert(new Result(next));
p1.podUcer(convert,channel);
}else{
System.out.println("邮件格式不正确");
}
} catch (Exception e) {
System.out.println("邮件发送失败");
}
}}
}
class Producerq {
public void podUcer(String src,Channel channel) throws IOException, TimeoutException {
//发送消息
channel.basicPublish("","sendmail",null,src.getBytes()); //消息发送
System.out.println("邮件发送成功!,请稍后!(单次时间10秒)");
}
}
class JsonZH { //json格式转换器 7
public static String convert(Result result){ //传入需要转换的json格式
return JSON.toJSONString(result); //传出去json格式
}
}