我的思路是借用數(shù)據(jù)庫(kù):
秀山土家族苗族網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),秀山土家族苗族網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為秀山土家族苗族成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的秀山土家族苗族做網(wǎng)站的公司定做!
發(fā)郵件,記錄一條數(shù)據(jù),并記錄發(fā)送時(shí)間
打開連接時(shí),執(zhí)行一次ajax查找,去數(shù)據(jù)庫(kù)讀取這次記錄的時(shí)間,然后比較。
當(dāng)然有一個(gè)不安全做法,但簡(jiǎn)單:
1.url后帶一個(gè)參數(shù)time=當(dāng)前時(shí)間的long值
2url打開后自行讀取這個(gè)long,再new Date一下進(jìn)行比較。注意用到服務(wù)器的date
小公司用javamail就行了 大公司看你的操作系統(tǒng) 要是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.前提準(zhǔn)備工作:
首先,郵件的發(fā)送方要開啟POP3 和SMTP服務(wù)--即發(fā)送qq郵件的賬號(hào)要開啟POP3 和SMTP服務(wù)
2.開啟方法:
登陸qq郵箱
3.點(diǎn)擊 設(shè)置
4.點(diǎn)擊—-賬戶
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務(wù) —點(diǎn)擊開啟
6.送短信 —–點(diǎn)擊確定
7.稍等一會(huì),很得到一個(gè)授權(quán)碼! –注意:這個(gè)一定要記住,一會(huì)用到
8.點(diǎn)擊保存修改 —OK 完成
9.java 測(cè)試代碼:
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");// 連接協(xié)議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機(jī)名
properties.put("mail.smtp.port", 465);// 端口號(hào)
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//設(shè)置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設(shè)置是否顯示debug信息 true 會(huì)在控制臺(tái)顯示相關(guān)信息
//得到回話對(duì)象
Session session = Session.getInstance(properties);
// 獲取郵件對(duì)象
Message message = new MimeMessage(session);
//設(shè)置發(fā)件人郵箱地址
message.setFrom(new InternetAddress("123456789@qq.com"));
//設(shè)置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });
//設(shè)置郵件標(biāo)題
message.setSubject("這是第一封Java郵件");
//設(shè)置郵件內(nèi)容
message.setText("內(nèi)容為: 這是第一封java發(fā)送來(lái)的郵件。");
//得到郵差對(duì)象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密碼為剛才得到的授權(quán)碼
//發(fā)送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運(yùn)行就會(huì)發(fā)出郵件了。。。。
下面是我收到郵件的截圖,當(dāng)然我把源碼中的郵件地址都是修改了,不是真實(shí)的,你們測(cè)試的時(shí)候,可以修改能你們自己的郵箱。最后,祝你也能成功,如果有什么問(wèn)題,可以一起討論!
注意事項(xiàng)
得到的授權(quán)碼一定要保存好,程序中要使用