程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java編寫簡略的E-mail發送端法式

java編寫簡略的E-mail發送端法式

編輯:關於JAVA

java編寫簡略的E-mail發送端法式。本站提示廣大學習愛好者:(java編寫簡略的E-mail發送端法式)文章只能為提供參考,不一定能成為您想要的結果。以下是java編寫簡略的E-mail發送端法式正文


本文實例引見了簡略E-mail發送端法式的Java完成代碼,分享給年夜家供年夜家參考,詳細內容以下

在這個代碼中,有幾個留意點強調一下:
1、應用 Socket 與 SMTP 郵件辦事器獲得銜接,留意 SMTP 辦事器的主機名;
2、應用 data 敕令時,若寫了 subject (主題)以後,郵件的注釋部門必需與 subject 之間有一個空行,即“回車+換行”,在代碼中則是 \r\n ;
3、異樣須要將發件人的郵箱用戶名、暗碼停止 BASE64 編碼以後再傳給 SMTP 辦事器;
4、法式在編譯時依然存在正告,這是因為 sun.misc.BASE64Encoder 類是存在於 rt.jar 包中的,因為 JDK 會更新進級,能夠會招致該包中的某些類產生變更而弗成用,所以編譯器會收回正告。
另外,寫了這些代碼,也發明了一些成績:
1、smtp.qq.com 和 smtp.sina.com 郵件辦事器不曉得為何不克不及用,也就是說,當發件人的郵箱地址是 qq 或許 sina 時,這個法式不論用了,狀況應對碼也看不懂。在我的測試傍邊,只要 smtp.163.com 可以用,明明都是在官網查到這幾個 SMTP 辦事器的,怎樣不克不及用呢?真奇異。有哪位同伙曉得的願望能告知我,感謝!
2、鄙人面的 SimpleMailSender 類中的 sendEmail() 辦法中,有些反復代碼讓人覺得困惑,然則沒方法,我臨時還弄不懂…
3、嚴重發明:QQ 郵箱吸收郵件的速度比 163 郵箱、sina 郵箱要快上能夠稀有十倍,真讓我驚奇。另外,應用 nslookup 敕令查詢 smtp.qq.com 的主機名時,發明它有很多多少台 SMTP 辦事器,至多比 163 的 3 台多出 5 台,騰訊真夠壯大;
4、固然說寫了這個法式可以歹意地一直給某個郵箱發郵件,然則我發明,當我用一個 sina 郵箱持續發送了幾十封郵件給固定的另外一個郵箱以後,該 sina  郵箱再想發郵件就會被謝絕,當心喲。
代碼以下:

// 郵件
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 類中停止用戶名、暗碼的轉碼任務
 */
 private String toBASE64(String str) {
 return (new sun.misc.BASE64Encoder().encode(str.getBytes())); 
 }
}
// 簡略的郵件發送端類,完成發送功效
public class SimpleMailSender {
 private String smtpServer;
 private int port = 25;

 private Socket socket;
 BufferedReader br;
 PrintWriter pw;
 
 /**
 * 依據發件人的郵箱地址肯定SMTP郵件辦事器 
 */
 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);
 
 // 開端組裝發送郵件的敕令序列
 send_Receive(null); // 吸收銜接SMTP辦事器勝利的信息
 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");
 
 // 郵件內容
 pw.println("from:" + email.from);
 pw.println("to:" + email.to);
 // 主題與注釋之間必定要空一行,即加上"\r\n"
 pw.println("subject:" + email.subject + "\r\n");
 
 // 在掌握台打印郵件內容
 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);
 
 // 必定記得注釋以"."停止
 send_Receive(".");
 send_Receive("quit");
 } catch (IOException e) {
 e.printStackTrace();
 } finally {
 try {
 if (socket != null)
  socket.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }
 
 /**
 * 每發送一條敕令,必需在敕令前面加上"\r\n",
 * 則同時打印出smtp郵件辦事器的響應狀況碼
 * @param command
 */
 private void send_Receive(String command) throws IOException{
 if(command != null) {
 // 向SMTP郵件辦事器發送死令,必定要記得加上"\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();
 }
}
// 郵件發送法式界面
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("發件人郵箱地址:");
 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("螞蟻-->簡略郵件發送器");
 this.setBounds(200, 30, 500, 500);
 this.setLayout(new BorderLayout());
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 this.setVisible(true);

 this.sendBtn = new JButton("發送");
 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發送端法式的全體代碼,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved