郵箱服務器

一.郵箱服務器的基本概念

郵件的客戶端:可以只安裝在電腦上(C/S)的也可以是網頁形式(B/S)的
郵件服務器:起到郵件的接受與推送的作用
郵件發送的協議:
協議:就是數據傳輸的約束
接受郵件的協議:POP3 、 IMAP
發送郵件的協議:SMTP
網易郵箱服務器地址:
pop.126.com
smtp.126.com
imap.126.com

郵件發送與接收的詳細過程

在這里插入圖片描述

二,demo—做一個生日祝福定時發送

package beyond.mail;import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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 MailUtils {//email:郵件要發給誰(收件人)//subject:代表主題//emailMsg:郵件的內容public static void sendMail(String email, String subject,String emailMsg)throws AddressException, MessagingException {// 1.創建一個程序與郵件服務器會話對象 SessionProperties props = new Properties();props.setProperty("mail.transport.protocol", "SMTP");//發郵件的協議props.setProperty("mail.host", "localhost");//發送郵件的服務器地址props.setProperty("mail.smtp.auth", "true");// 指定驗證為true// 創建驗證器Authenticator auth = new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication("yan", "beyond");//發郵件的賬號驗證}};Session session = Session.getInstance(props, auth);// 2.創建一個Message,它相當于是郵件內容Message message = new MimeMessage(session);message.setFrom(new InternetAddress("yan@yy.com")); // 設置發送者message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設置發送方式與接收者message.setSubject(subject);//設置郵件的主題// message.setText("這是一封激活郵件,請<a href='#'>點擊</a>");message.setContent(emailMsg, "text/html;charset=utf-8");// 3.創建 Transport用于將郵件發送Transport.send(message);}
}
package beyond.birthday;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DataSourceUtils {private static DataSource dataSource = new ComboPooledDataSource();private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();// 直接可以獲取一個連接池public static DataSource getDataSource() {return dataSource;}public static Connection getConnection() throws SQLException{return dataSource.getConnection();}// 獲取連接對象public static Connection getCurrentConnection() throws SQLException {Connection con = tl.get();if (con == null) {con = dataSource.getConnection();tl.set(con);}return con;}// 開啟事務public static void startTransaction() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.setAutoCommit(false);}}// 事務回滾public static void rollback() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.rollback();}}// 提交并且 關閉資源及從ThreadLocall中釋放public static void commitAndRelease() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.commit(); // 事務提交con.close();// 關閉資源tl.remove();// 從線程綁定中移除}}// 關閉資源方法public static void closeConnection() throws SQLException {Connection con = getCurrentConnection();if (con != null) {con.close();}}public static void closeStatement(Statement st) throws SQLException {if (st != null) {st.close();}}public static void closeResultSet(ResultSet rs) throws SQLException {if (rs != null) {rs.close();}}}
web.xml
<listener><listener-class>beyond.birthday.BirthdayListener</listener-class>	</listener>
package beyond.birthday;public class Customer {private int id;private String username;private String password;private String realname;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRealname() {return realname;}public void setRealname(String realname) {this.realname = realname;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}private String birthday;private String email;}
package beyond.birthday;import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;import javax.mail.MessagingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;import beyond.birthday.Customer;
import beyond.mail.MailUtils;public class BirthdayListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {//當web應用啟動開啟調動---功能在用戶的生日當前發送郵件Timer timer = new Timer();//開啟定時器,導util下的包timer.scheduleAtFixedRate(new TimerTask() {@Overridepublic void run() {//為當前生日用戶發郵件//1,獲得當前過生日的用戶SimpleDateFormat format = new SimpleDateFormat("MM-dd");//獲得當天的日期String currentDate = format.format(new Date());//currentDate----10-14QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());//根據當前的時間,從數據庫查詢今天過生日的用戶String sql = "select * from customer where birthday like ?";//寫SQL語句List<Customer> customerList = null;try {customerList = runner.query(sql, new BeanListHandler<Customer>(Customer.class),"%"+currentDate);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//2,發郵件給當前過生日的用戶if(customerList!=null&&customerList.size()>0){for(Customer c :customerList){String emailMsg = "親愛的:" + c.getRealname() + ",生日快樂!";try {MailUtils.sendMail(c.getEmail(), "生日祝福", emailMsg);System.out.println(c.getRealname()+"郵件發送完畢");} catch (MessagingException e) {e.printStackTrace();}}}}}, new Date(),1000*30 );//1000ms*30=半分鐘//實際開發中,起始時間肯定是一個固定的時間//period:間隔時間  時間開發中,間隔時間是1天(為一天會有好多人過生日,把今天過生日的人給發送生日祝福)}@Overridepublic void contextDestroyed(ServletContextEvent sce) {// TODO Auto-generated method stub}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379497.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379497.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379497.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C#提高保存jpg圖像的質量

在程序中直接生成的jpg圖像&#xff0c;漢字有毛邊&#xff0c;經過一番搜索&#xff0c;在msdn上發現了下面控制jpg質量系數的文章&#xff0c;修改后試了一下&#xff0c;效果確實比前面強多了。原理我也不大懂&#xff0c;把代碼貼出來&#xff0c;與大家共享。 聯合圖…

延遲和定時器管理

文章目錄1 內核中時間概念2 標準定時器jiffies和HZ定時器API標準定時器案例3 高精度定時器(HRT)高精度定時器案例4 內核中延遲和睡眠原子上下文非原子上下文1 內核中時間概念 時間概念對計算機來說有些模糊&#xff0c;事實上內核必須在硬件的幫助下才能計算和管理時間。硬件為…

Web開發工具(插件)收集

1.IE Developer Toolbar 瀏覽和修改&#xff0c;選定Web頁上的特定元素&#xff0c;查看HTML對象的類名、ID&#xff0c;以及類似鏈接路徑、tab順序、快捷鍵等。 2.HttpWatch Professional 一款強大的網頁數據分析工具,可以查看當前網頁的http數據 FireFox插件 FireFox下插件實…

cin、cin.get()、cin.getline()、getline()、gets()等函數的用法

轉載&#xff0c;并經過本人補充cin、cin.get()、cin.getline()、getline()、gets()等函數的用法2007/10/27 22:51學C的時候&#xff0c;這幾個輸入函數弄的有點迷糊&#xff1b;這里做個小結&#xff0c;為了自己復習&#xff0c;也希望對后來者能有所幫助&#xff0c;如果有差…

Java StringBuilder subSequence()方法與示例

StringBuilder類subSequence()方法 (StringBuilder Class subSequence() method) subSequence() method is available in java.lang package. subSequence()方法在java.lang包中可用。 subSequence() method is used to return the new set of a character sequence that is a …

Linux設備驅動開發---設備樹的概念

文章目錄1 設備樹機制命名約定別名、標簽和phandleDT編譯器2 表示和尋址設備SPI和I2C尋址平臺設備尋址3 處理資源提取特定應用數據文本字符串單元格和無符號的32位整數布爾提取并分析子節點4 平臺驅動程序與DTOF匹配風格處理非設備樹平臺平臺數據與DT設備樹&#xff08;DT&…

【轉】C#中數組復制的4種方法

C#中數組復制的4種方法 from&#xff1a;http://blog.csdn.net/burningcpu/article/details/1434167今天旁邊的同事MM叫我調了一段程序&#xff0c;她想復制一個數組&#xff0c;int[] pins {9,3,4,9};int [] alias pins;這里出了錯誤&#xff0c;也是錯誤的根源&#xff0c…

Java StringBuilder codePointAt()方法與示例

StringBuilder類codePointAt()方法 (StringBuilder Class codePointAt() method) codePointAt() method is available in java.lang package. codePointAt()方法在java.lang包中可用。 codePointAt() method is used to return the Unicode code point at the given indices an…

用戶虛擬地址轉化成物理地址,物理地址轉換成內核虛擬地址,內核虛擬地址轉換成物理地址,虛擬地址和對應頁的關系

文章目錄1. 用戶虛擬地址轉換成物理地址2. 內核虛擬地址轉換成物理地址3. 物理地址轉換成內核虛擬地址4 內核虛擬地址和對應頁5 根據進程號獲取進程描述符1. 用戶虛擬地址轉換成物理地址 static void get_pgtable_macro(void) {printk("PAGE_OFFSET 0x%lx\n", PAGE…

簡單三層架構(登錄)

1&#xff0c;首先導包 dao //獲取數據String username request.getParameter("username");String password request.getParameter("password");//傳遞到Service層UserService service new UserService();//這里的UserService 需要創建到service包下Use…

通過隱藏option實現select的聯動效果

開始的時候需求是根據一定條件隱藏一部分<option>標簽&#xff0c;類似聯動效果&#xff0c;但是目前的html規范并沒有為<option>提供隱藏的效果&#xff0c;因此常用的設置display或者visibility無效。網上大部分解決方案是刪除<option>節點或<option>…

Java SimpleTimeZone setEndRule()方法與示例

SimpleTimeZone類setEndRule()方法 (SimpleTimeZone Class setEndRule() method) Syntax: 句法&#xff1a; public void setEndRule(int en_mm, int en_dd, int en_time);public void setEndRule(int en_mm, int en_dd, int en_dow, int en_time);public void setEndRule(int…

Linux設備驅動開發--- DMA

文章目錄1 設置DMA映射緩存一致性和DMADMA映射一致映射流式DMA映射2 完成的概念3 DMA引擎API分配DMA從通道設置從設備和控制器指定參數獲取事務描述符提交事務發布待處理DMA請求并等待回調通知4 程序單緩沖區映射分散聚集映射DMA是計算機系統的一項功能&#xff0c;它允許設備在…

類加載器

一、類加載器 1&#xff0c;什么是類加載器&#xff1f; 類加載器就是用來加載字節碼文件 2&#xff0c;類加載器的種類有哪些&#xff1f; 1&#xff09;BootStrap&#xff1a;引導類加載器&#xff1a;加載都是最基礎的文件 2&#xff09;ExtClassLoader&#xff1a;擴展類加…

一個用java讀取XML文件的簡單方法(轉)

XML文件 book.xml <book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first> <last>Gates</last> <age>46</age&g…

Java ObjectStreamField getName()方法與示例

ObjectStreamField類的getName()方法 (ObjectStreamField Class getName() method) getName() method is available in java.io package. getName()方法在java.io包中可用。 getName() method is used to get the name of this ObjectStreamField field. getName()方法用于獲取…

【css】CSS中折疊margin的問題

為什么要翻譯這篇說明&#xff1f;css2本有人已翻譯過&#xff0c;但看一下&#xff0c;很粗糙&#xff08;不是說自己就怎么怎么樣啊&#xff0c;翻譯者真的是很值得敬佩的&#xff01;&#xff09;&#xff0c;近來跟css與xhtml接觸得越來越多&#xff0c;但接觸得越多&#…

算法---鏈表

文章目錄反轉鏈表合并兩個有序鏈表刪除重復元素反轉鏈表 反轉鏈表包括兩種&#xff0c;反轉全部元素或者反轉部分元素。在這里&#xff0c;我們約定&#xff1a;數據元素類型是struct LinkNode&#xff0c;要反轉鏈表的第一個節點是head&#xff0c;head的前面一個節點是pre&a…

SSM

二、環境設置&#xff08;MyEclipse&#xff09; 1&#xff0c;字體設置 window–>Preference->General->Appearance->Colors and Fonts->Basic Text->Font 2&#xff0c;workspace字符集設置 window–>Preference->General->Appearance->W…

IOS NSArray,NSDictionary

小結&#xff1a; NSArray有序的集合&#xff1b; NSDictionary無序的集合&#xff0c;可排序&#xff1b; 增刪改查 ------NSArray----------- create : 1)NSArray *array [NSArray arrayWithObjects:"Henry","Jones", "Susan", "Smith&q…