最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java編寫簡單的E-mail發(fā)送端程序

 更新時(shí)間:2016年02月29日 15:33:10   作者:螞蟻  
這篇文章主要介紹了使用java語言編寫一個(gè)簡單的E-mail發(fā)送端程序,感興趣的小伙伴們可以參考一下

本文實(shí)例介紹了簡單E-mail發(fā)送端程序的Java實(shí)現(xiàn)代碼,分享給大家供大家參考,具體內(nèi)容如下

在這個(gè)代碼中,有幾個(gè)注意點(diǎn)強(qiáng)調(diào)一下
1、使用 Socket 與 SMTP 郵件服務(wù)器取得連接,注意 SMTP 服務(wù)器的主機(jī)名;
2、使用 data 命令時(shí),若寫了 subject (主題)之后,郵件的正文部分必須與 subject 之間有一個(gè)空行,即“回車+換行”,在代碼中則是 \r\n ;
3、同樣需要將發(fā)件人的郵箱用戶名、密碼進(jìn)行 BASE64 編碼之后再傳給 SMTP 服務(wù)器;
4、程序在編譯時(shí)仍然存在警告,這是由于 sun.misc.BASE64Encoder 類是存在于 rt.jar 包中的,由于 JDK 會(huì)更新升級(jí),可能會(huì)導(dǎo)致該包中的某些類發(fā)生變化而不可用,所以編譯器會(huì)發(fā)出警告。
此外,寫了這些代碼,也發(fā)現(xiàn)了一些問題
1、smtp.qq.com 和 smtp.sina.com 郵件服務(wù)器不知道為什么不能用,也就是說,當(dāng)發(fā)件人的郵箱地址是 qq 或者 sina 時(shí),這個(gè)程序不管用了,狀態(tài)應(yīng)答碼也看不懂。在我的測試當(dāng)中,只有 smtp.163.com 可以用,明明都是在官網(wǎng)查到這幾個(gè) SMTP 服務(wù)器的,怎么不能用呢?真奇怪。有哪位朋友知道的希望能告訴我,謝謝!
2、在下面的 SimpleMailSender 類中的 sendEmail() 方法中,有些重復(fù)代碼讓人感到疑惑,但是沒辦法,我暫時(shí)還弄不懂…
3、重大發(fā)現(xiàn):QQ 郵箱接收郵件的速度比 163 郵箱、sina 郵箱要快上可能有數(shù)十倍,真讓我驚訝。此外,使用 nslookup 命令查詢 smtp.qq.com 的主機(jī)名時(shí),發(fā)現(xiàn)它有好多臺(tái) SMTP 服務(wù)器,至少比 163 的 3 臺(tái)多出 5 臺(tái),騰訊真夠強(qiáng)大;
4、雖然說寫了這個(gè)程序可以惡意地不停給某個(gè)郵箱發(fā)郵件,但是我發(fā)現(xiàn),當(dāng)我用一個(gè) sina 郵箱連續(xù)發(fā)送了幾十封郵件給固定的另一個(gè)郵箱之后,該 sina  郵箱再想發(fā)郵件就會(huì)被拒絕,小心喲。
代碼如下:

// 郵件
class E_Mail {
 String from;
 String to;
 String subject;
 String content;
 String userName;
 String pwd;

 public E_Mail(String from, String to, String subject, 
 String content, String userName, String pwd) {
 this.from = from;
 this.to = to;
 this.subject = subject;
 this.content = content;
 this.userName = this.toBASE64(userName);
 this.pwd = this.toBASE64(pwd);
 }
 
 /**
 * 在 E_Mail 類中進(jìn)行用戶名、密碼的轉(zhuǎn)碼工作
 */
 private String toBASE64(String str) {
 return (new sun.misc.BASE64Encoder().encode(str.getBytes())); 
 }
}
// 簡單的郵件發(fā)送端類,實(shí)現(xiàn)發(fā)送功能
public class SimpleMailSender {
 private String smtpServer;
 private int port = 25;

 private Socket socket;
 BufferedReader br;
 PrintWriter pw;
 
 /**
 * 根據(jù)發(fā)件人的郵箱地址確定SMTP郵件服務(wù)器 
 */
 private void initServer(String from) {
 if(from.contains("@163")) {
 this.smtpServer = "smtp.163.com";
 }else if(from.contains("@126")) {
 this.smtpServer = "smtp.126.com";
 }else if(from.contains("@sina")) {
 this.smtpServer = "smtp.sina.com";
 }else if(from.contains("@qq")) {
 this.smtpServer = "smtp.qq.com";
 }
 }

 public void sendEmail(E_Mail email) {
 try {
 this.initServer(email.from);
 
 this.socket = new Socket(smtpServer, port);
 this.br = this.getReader(socket);
 this.pw = this.getWriter(socket);
 
 // 開始組裝發(fā)送郵件的命令序列
 send_Receive(null); // 接收連接SMTP服務(wù)器成功的信息
 send_Receive("ehlo hao");
 send_Receive("auth login");
 send_Receive(email.userName);
 send_Receive(email.pwd);
 send_Receive("mail from:<" + email.from + ">");
 send_Receive("rcpt to:<" + email.to + ">");
 send_Receive("data");
 
 // 郵件內(nèi)容
 pw.println("from:" + email.from);
 pw.println("to:" + email.to);
 // 主題與正文之間一定要空一行,即加上"\r\n"
 pw.println("subject:" + email.subject + "\r\n");
 
 // 在控制臺(tái)打印郵件內(nèi)容
 System.out.println("from:" + email.from);
 System.out.println("to:" + email.to); 
 System.out.println("subject:" + email.subject + "\r\n");
 System.out.println(email.content);
 
 // 郵件正文
 pw.println(email.content);
 
 // 一定記得正文以"."結(jié)束
 send_Receive(".");
 send_Receive("quit");
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 try {
 if (socket != null)
  socket.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 
 /**
 * 每發(fā)送一條命令,必須在命令后面加上"\r\n",
 * 則同時(shí)打印出smtp郵件服務(wù)器的相應(yīng)狀態(tài)碼
 * @param command
 */
 private void send_Receive(String command) throws IOException{
 if(command != null) {
 // 向SMTP郵件服務(wù)器發(fā)送命令,一定要記得加上"\r\n"
 pw.print(command + "\r\n");
 pw.flush();
 System.out.println("用戶 >> " + command);
 }
 
 char [] response = new char[1024];
 br.read(response);
 System.out.println(response);
 }

 /**
 * 獲取 Socket 的輸出流
 */
 private PrintWriter getWriter(Socket socket) throws IOException {
 OutputStream socketOut = socket.getOutputStream();
 return new PrintWriter(socketOut, true);
 }

 /**
 * 獲取 Socket 的輸入流
 */
 private BufferedReader getReader(Socket socket) throws IOException {
 InputStream socketIn = socket.getInputStream();
 return new BufferedReader(new InputStreamReader(socketIn));
 }

 // 測試
 public static void main(String[] args) {
 new MailSenderGUI();
 }
}
// 郵件發(fā)送程序界面
class MailSenderGUI extends JFrame implements ActionListener {
 private JLabel userNameLabel;
 private JTextField userNameField;
 private JLabel pwdLabel;
 private JPasswordField pwdField;
 private JLabel fromLabel;
 private JTextField fromField;
 private JLabel toLabel;
 private JTextField toField;
 private JLabel subjectLabel;
 private JTextField subjectField;
 private JLabel contentLabel;
 private JTextArea contentArea;

 private JButton sendBtn;
 private JButton cancelBtn;

 private E_Mail email;

 private SimpleMailSender mailSender;

 public MailSenderGUI() {
 this.init();
 this.mailSender = new SimpleMailSender();
 }

 private void init() {
 this.fromLabel = new JLabel("發(fā)件人郵箱地址:");
 this.fromField = new JTextField(25);
 this.userNameLabel = new JLabel("用戶名:");
 this.userNameField = new JTextField(25);
 this.pwdLabel = new JLabel("密碼:");
 this.pwdField = new JPasswordField(25);
 this.toLabel = new JLabel("收件人郵箱地址:");
 this.toField = new JTextField(25);
 this.subjectLabel = new JLabel("郵件主題:");
 this.subjectField = new JTextField(20);
 this.contentLabel = new JLabel("郵件正文:");
 this.contentArea = new JTextArea(15, 20);

 this.setTitle("螞蟻-->簡單郵件發(fā)送器");
 this.setBounds(200, 30, 500, 500);
 this.setLayout(new BorderLayout());
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setVisible(true);

 this.sendBtn = new JButton("發(fā)送");
 this.cancelBtn = new JButton("重置");

 this.sendBtn.addActionListener(this);
 this.cancelBtn.addActionListener(this);
 
 JPanel upPanel = new JPanel(new GridLayout(6, 2, 5, 5));
 upPanel.add(fromLabel);
 upPanel.add(fromField);
 upPanel.add(userNameLabel);
 upPanel.add(userNameField);
 upPanel.add(pwdLabel);
 upPanel.add(pwdField);
 upPanel.add(toLabel);
 upPanel.add(toField);
 upPanel.add(subjectLabel);
 upPanel.add(subjectField);
 upPanel.add(contentLabel);
 
 this.add(upPanel, BorderLayout.NORTH); 
 this.add(contentArea, BorderLayout.CENTER);
 
 JPanel downPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
 downPanel.add(sendBtn, BorderLayout.SOUTH);
 downPanel.add(cancelBtn, BorderLayout.SOUTH);
 
 this.add(downPanel, BorderLayout.SOUTH);
 }

 @Override
 public void actionPerformed(ActionEvent e) {
 if (e.getSource() == this.sendBtn) {
 this.email = new E_Mail(
  this.fromField.getText(),
  this.toField.getText(), 
  this.subjectField.getText(),
  this.contentArea.getText(),
  this.userNameField.getText(),
  new String(this.pwdField.getPassword())
  );

 this.mailSender.sendEmail(this.email);

 } else if (e.getSource() == this.cancelBtn) {
 this.fromField.setText(null);
 this.toField.setText(null);
 this.subjectField.setText(null);
 this.contentArea.setText(null);
 }

 }
}

以上就是java編寫簡單E-mail發(fā)送端程序的全部代碼,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決

    spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決

    這篇文章主要介紹了spring-boot中spring-boot-maven-plugin報(bào)紅錯(cuò)誤及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java實(shí)現(xiàn)一個(gè)簡單TCPSocket聊天室功能分享

    java實(shí)現(xiàn)一個(gè)簡單TCPSocket聊天室功能分享

    這篇文章主要為大家分享了java實(shí)現(xiàn)的一個(gè)簡單TCPSocket聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • 詳談Java枚舉、靜態(tài)導(dǎo)入、自動(dòng)拆裝箱、增強(qiáng)for循環(huán)、可變參數(shù)

    詳談Java枚舉、靜態(tài)導(dǎo)入、自動(dòng)拆裝箱、增強(qiáng)for循環(huán)、可變參數(shù)

    下面小編就為大家?guī)硪黄斦凧ava枚舉、靜態(tài)導(dǎo)入、自動(dòng)拆裝箱、增強(qiáng)for循環(huán)、可變參數(shù)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Spring AOP手動(dòng)實(shí)現(xiàn)簡單動(dòng)態(tài)代理的代碼

    Spring AOP手動(dòng)實(shí)現(xiàn)簡單動(dòng)態(tài)代理的代碼

    今天小編就為大家分享一篇關(guān)于Spring AOP手動(dòng)實(shí)現(xiàn)簡單動(dòng)態(tài)代理的代碼,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目

    eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目

    本文主要介紹了eclipse啟動(dòng)一個(gè)Springboot項(xiàng)目,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解

    java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解

    這篇文章主要介紹了java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解,jar包實(shí)現(xiàn)了特定功能的,java字節(jié)碼文件的壓縮包,更多相關(guān)內(nèi)容需要的朋友可以參考一下
    2022-07-07
  • java ArrayList中的remove方法介紹

    java ArrayList中的remove方法介紹

    大家好,本篇文章主要講的是java ArrayList中的remove方法介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • 詳解MyBatis-Plus updateById方法更新不了空字符串/null解決方法

    詳解MyBatis-Plus updateById方法更新不了空字符串/null解決方法

    這篇文章主要介紹了詳解MyBatis-Plus updateById方法更新不了空字符串/null解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java面試必備之JMM高并發(fā)編程詳解

    Java面試必備之JMM高并發(fā)編程詳解

    高并發(fā)(High?Cuncurrency)是互聯(lián)網(wǎng)分布式系統(tǒng)架構(gòu)設(shè)計(jì)中必須考慮的因素之一,它通常是指,通過設(shè)計(jì)保證系統(tǒng)能夠同時(shí)并發(fā)處理很多請(qǐng)求
    2022-07-07
  • Java上傳視頻實(shí)例代碼

    Java上傳視頻實(shí)例代碼

    本文通過實(shí)例代碼給大家講解了java上傳視頻功能,代碼分為頁面前臺(tái)和后臺(tái),工具類,具體實(shí)例代碼大家通過本文學(xué)習(xí)吧
    2018-01-01

最新評(píng)論

桐乡市| 云阳县| 漠河县| 徐闻县| 武汉市| 安化县| 乌拉特后旗| 阿拉善右旗| 沐川县| 奉化市| 连州市| 天峻县| 汝阳县| 香港 | 科技| 读书| 台州市| 呼图壁县| 呈贡县| 墨江| 南昌市| 嘉峪关市| 永修县| 安顺市| 微博| 寿光市| 呼图壁县| 双城市| 鸡东县| 兴化市| 泗洪县| 黑河市| 林甸县| 成安县| 抚宁县| 肃宁县| 平定县| 西和县| 新巴尔虎右旗| 桓台县| 泰州市|