📄 bmailsend.java
字号:
/*
* Created on 2005-5-26
* @author zhangh
* This software is the proprietary information of nsct, Inc. all the code are
* for the net of www.coolcen.com.
*/
package com.bookstore.util;
import java.util.Properties;
import java.util.Date;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class BMailSend {
String host = "smtp.163.com";
String user = "zhbj6";
String password = "346578019";
Properties props = null;
Session mailSession = null;
Message message = null;
public void setHost(String host) {
this.host = host;
}
public BMailSend() {
Properties props = new Properties();
props.put("mail.smtp.host", host); //指定SMTP服务器
props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true); //是否在控制台显示debug信息
message = new MimeMessage(mailSession);
}
public void setAccount(String user, String password) {
this.user = user;
this.password = password;
}
public void setFrom(String from) throws Exception {
message.setFrom(new InternetAddress(from)); //发件人
}
public void setTo(String to) throws Exception {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//收件人
}
public void setTitle(String title) throws Exception {
message.setSubject(title); //邮件主题
}
public void setContentToHTML(String content,boolean ishtml) throws Exception {
if(ishtml)
message.setContent(content,"text/html");
else
message.setText(content); //邮件内容
}
public void send() {
try {
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[])throws Exception{
//BMailSend sm = new BMailSend();
//sm.setFrom("zhbj6@163.com");
//sm.setTo("zhbj6@126.com");
//sm.setTitle("感谢您注册海上书仓,请激活您的帐号!");
//sm.setContentToHTML("<html><body><font color=red><b>i love you</b></font></body></html>",true);
//sm.send();
//求出明天的具体时间
java.text.SimpleDateFormat formatter =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date mydate = new Date();
System.out.println("mydate=="+mydate);
long myTime=(mydate.getTime()/1000)+60*60*24;
mydate.setTime(myTime*1000);
String mDate = formatter.format(mydate);
System.out.println(mDate);
//求两个时间的天数
java.util.Date date= formatter.parse("2003-05-1 12:12:12");//相当于用户注册时的时间
java.util.Date todate= formatter.parse("2003-05-2 13:12:12");//相当于明天的时间
long day=(todate.getTime()-date.getTime())/(24*60*60*1000);
System.out.println(day);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -