本文實例介紹了簡單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發送端程序的全部代碼,希望對大家的學習有所幫助。