我有每次发送告警后将收件人的地址和发送的告警的id保存到Session中了,前边加了判断如果是收件人地址和收到的告警id和上次相同就不在发送可是我运行的时候还是不能保证不重复发送,是不是代码写的有问题啊,各位给看看啊!!
public static void transport(String id){
String mailinfoArry[][]=(String[][]) SessionUtil.get("mailinfoArry");
for(int i=0;i<mailinfoArry.length;i++){
String toAddr = mailinfoArry[i][0];//收件人地址
String level = mailinfoArry[i][1]; //告警级别

Properties props = new Properties(); SendMailAuthenticator au = new SendMailAuthenticator();
au.check("****", "****"); // 认证 (用户名和密码)
props.put("mail.smtp.host", "smtp.126.com"); // 设置smtp服务器
props.put("mail.smtp.auth", "true"); // 这样认证才会起作用:)
Session session = Session.getInstance(props, au); MimeMessage message = new MimeMessage(session); 
try {
if(id!=SessionUtil.get("AlarmId")||!(toAddr.equals( SessionUtil.get("sendmailuser")))){       
Address address = new InternetAddress("[email protected]", "***"); //发件人地址
Address toAddress = new InternetAddress(toAddr);//收件人地址
message.setFrom(address);//设置发件人
message.setRecipient(MimeMessage.RecipientType.TO, toAddress);//设置收件人 //下面写邮件内容
message.setSubject("测试邮件", "GB2312"); //设置主题
message.setSentDate(new Date());//设置日期
message.setText("凡在我心目中占有一定地位的人都将获得我有我免费提供测试邮件一封!!呵呵…^_^," +
"发生告警的设备id是:"+id, "GB2312");//设置邮件内容 //发送啦
Transport transport = session.getTransport("smtp");
Transport.send(message);
            System.out.println("发送邮件:"+toAddr+"    "+id);
            SessionUtil.set("sendmailuser",toAddr);//把已发送邮件的收件人地址写入内存sendmailuser里
            SessionUtil.set("AlarmId",id);         //发生告警信息的设备的id写入内存
}
            
            
} catch (Exception ex) { 
ex.printStackTrace(); }

}
}