我的思路是借用数据库:
秀山土家族苗族网站建设公司成都创新互联,秀山土家族苗族网站设计制作,有大型网站制作公司丰富经验。已为秀山土家族苗族成百上千提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的秀山土家族苗族做网站的公司定做!
发邮件,记录一条数据,并记录发送时间
打开连接时,执行一次ajax查找,去数据库读取这次记录的时间,然后比较。
当然有一个不安全做法,但简单:
1.url后带一个参数time=当前时间的long值
2url打开后自行读取这个long,再new Date一下进行比较。注意用到服务器的date
小公司用javamail就行了 大公司看你的操作系统 要是Linux的话推荐用postfix Windows的话推荐用exchange。
附上exchange源码要使用的话需要加包
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Mailer {
private String host;
private String auth;
private String username;
private String domainUser;
private String password;
public boolean send(String[] to, String[] cc, String[] bcc, String subject, String content) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", auth);
Session s = Session.getInstance(props);
//s.setDebug(true);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(username);
message.setFrom(from);
//e.printStackTrace();
//message.setFrom(from);
InternetAddress[] Toaddress = new InternetAddress[to.length];
for (int i = 0; i to.length; i++)
Toaddress[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, Toaddress);
if (cc != null) {
InternetAddress[] Ccaddress = new InternetAddress[cc.length];
for (int i = 0; i cc.length; i++)
Ccaddress[i] = new InternetAddress(cc[i]);
message.setRecipients(Message.RecipientType.CC, Ccaddress);
}
if (bcc != null) {
InternetAddress[] Bccaddress = new InternetAddress[bcc.length];
for (int i = 0; i bcc.length; i++)
Bccaddress[i] = new InternetAddress(bcc[i]);
message.setRecipients(Message.RecipientType.BCC, Bccaddress);
}
message.setSubject(subject);
message.setSentDate(new Date());
BodyPart mdp = new MimeBodyPart();
mdp.setContent(content, "text/html;charset=utf-8");
Multipart mm = new MimeMultipart();
mm.addBodyPart(mdp);
message.setContent(mm);
message.saveChanges();
Transport transport = s.getTransport("smtp");
transport.connect(host, (null == domainUser) ? username : domainUser, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
}
public Mailer(String host, String auth, String domainUser, String username, String password) {
super();
this.host = host;
this.auth = auth;
this.domainUser = domainUser;
this.username = username;
this.password = password;
}
public static void main(String[]args){
try {
new Mailer("你的ip", "true", "域名\\域用户", "邮件", "密码").send(new String[] { "281683400@qq.com" }, null, null, "demo_title", "h3test/h3");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
方法:
1.前提准备工作:
首先,邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务
2.开启方法:
登陆qq邮箱
3.点击 设置
4.点击—-账户
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启
6.送短信 —–点击确定
7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到
8.点击保存修改 —OK 完成
9.java 测试代码:
package cn.cupcat.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
properties.put("mail.smtp.port", 465);// 端口号
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties);
// 获取邮件对象
Message message = new MimeMessage(session);
//设置发件人邮箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//设置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//设置邮件标题
message.setSubject("这是第一封Java邮件");
//设置邮件内容
message.setText("内容为: 这是第一封java发送来的邮件。");
//得到邮差对象
Transport transport = session.getTransport();
//连接自己的邮箱账户
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码
//发送邮件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.运行就会发出邮件了。。。。
下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!
注意事项
得到的授权码一定要保存好,程序中要使用