使用hmailserver搭建邮件服务器
HMailServer是一个运行于微软Windows系统,基于GPL授权且免费的电子邮件系统;支持常见的电子邮件协议SMTP、POP3、IMAP,以下为操作过程相关截图:

点击其他设置:

使用java收发邮件
package com.itheima.mail; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.SendFailedException; import javax.mail.Session; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.sun.mail.smtp.SMTPAddressFailedException; import com.sun.mail.smtp.SMTPAddressSucceededException; import com.sun.mail.smtp.SMTPSendFailedException; import com.sun.mail.smtp.SMTPTransport; /** * 根据java mail examples 修改 * @author zhangming * @date 2016/10/13 */ public class Smtpsend { /** * Example of how to extend the SMTPTransport class. This example * illustrates how to issue the XACT command before the SMTPTransport issues * the DATA command. * * public static class SMTPExtension extends SMTPTransport { public * SMTPExtension(Session session, URLName url) { super(session, url); // to * check that we're being used * System.out.println("SMTPExtension: constructed"); } * * protected synchronized OutputStream data() throws MessagingException { if * (supportsExtension("XACCOUNTING")) issueCommand("XACT", 250); return * super.data(); } } */ public static void main(String[] argv) { String to = "user1@itheima.com", subject = "Test subject", from = "user2@itheima.com"; String mailhost = null; String mailer = "smtpsend"; String user = null, password = null; boolean debug = true; boolean verbose = false; boolean auth = false; String prot = "smtp"; new BufferedReader(new InputStreamReader(System.in)); try { /* * Initialize the JavaMail Session. */ Properties props = System.getProperties(); if (auth) props.put("mail." + prot + ".auth", "true"); /* * Create a Provider representing our extended SMTP transport and * set the property to use our provider. * * Provider p = new Provider(Provider.Type.TRANSPORT, prot, * "smtpsend$SMTPExtension", "JavaMail demo", "no version"); * props.put("mail." + prot + ".class", "smtpsend$SMTPExtension"); */ // Get a Session object Session session = Session.getInstance(props, null); if (debug) session.setDebug(true); /* * Register our extended SMTP transport. * * session.addProvider(p); */ /* * Construct the message and send it. */ Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); msg.setSubject(subject); String text = "This is a test message."; // If the desired charset is known, you can use // setText(text, charset) msg.setText(text); msg.setHeader("X-Mailer", mailer); msg.setSentDate(new Date()); // send the thing off /* * The simple way to send a message is this: * * Transport.send(msg); * * But we're going to use some SMTP-specific features for * demonstration purposes so we need to manage the Transport object * explicitly. */ SMTPTransport t = (SMTPTransport) session.getTransport(prot); try { if (auth) t.connect(mailhost, user, password); else t.connect(); t.sendMessage(msg, msg.getAllRecipients()); } finally { if (verbose) System.out.println("Response: " + t.getLastServerResponse()); t.close(); } System.out.println("\nMail was sent successfully."); } catch (Exception e) { /* * Handle SMTP-specific exceptions. */ if (e instanceof SendFailedException) { MessagingException sfe = (MessagingException) e; if (sfe instanceof SMTPSendFailedException) { SMTPSendFailedException ssfe = (SMTPSendFailedException) sfe; System.out.println("SMTP SEND FAILED:"); if (verbose) System.out.println(ssfe.toString()); System.out.println(" Command: " + ssfe.getCommand()); System.out.println(" RetCode: " + ssfe.getReturnCode()); System.out.println(" Response: " + ssfe.getMessage()); } else { if (verbose) System.out.println("Send failed: " + sfe.toString()); } Exception ne; while ((ne = sfe.getNextException()) != null && ne instanceof MessagingException) { sfe = (MessagingException) ne; if (sfe instanceof SMTPAddressFailedException) { SMTPAddressFailedException ssfe = (SMTPAddressFailedException) sfe; System.out.println("ADDRESS FAILED:"); if (verbose) System.out.println(ssfe.toString()); System.out.println(" Address: " + ssfe.getAddress()); System.out.println(" Command: " + ssfe.getCommand()); System.out .println(" RetCode: " + ssfe.getReturnCode()); System.out.println(" Response: " + ssfe.getMessage()); } else if (sfe instanceof SMTPAddressSucceededException) { System.out.println("ADDRESS SUCCEEDED:"); SMTPAddressSucceededException ssfe = (SMTPAddressSucceededException) sfe; if (verbose) System.out.println(ssfe.toString()); System.out.println(" Address: " + ssfe.getAddress()); System.out.println(" Command: " + ssfe.getCommand()); System.out .println(" RetCode: " + ssfe.getReturnCode()); System.out.println(" Response: " + ssfe.getMessage()); } } } else { System.out.println("Got Exception: " + e); if (verbose) e.printStackTrace(); } } } }
运行控制台输出:
DEBUG: setDebug: JavaMail version 1.5.4 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false 220 LENOVO-PC ESMTP DEBUG SMTP: connected to host "localhost", port: 25 EHLO Lenovo-PC 250-LENOVO-PC 250-SIZE 20480000 250-AUTH LOGIN 250 HELP DEBUG SMTP: Found extension "SIZE", arg "20480000" DEBUG SMTP: Found extension "AUTH", arg "LOGIN" DEBUG SMTP: Found extension "HELP", arg "" DEBUG SMTP: use8bit false MAIL FROM:<user2@itheima.com> 250 OK RCPT TO:<user1@itheima.com> 250 OK DEBUG SMTP: Verified Addresses DEBUG SMTP: user1@itheima.com DATA 354 OK, send. Date: Thu, 13 Oct 2016 23:00:02 +0800 (CST) From: user2@itheima.com To: user1@itheima.com Message-ID: <2108649164.0.1476370803054@Lenovo-PC> Subject: Test subject MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: smtpsend This is a test message. . 250 Queued (0.000 seconds) DEBUG SMTP: message successfully delivered to mail server QUIT 221 goodbye Mail was sent successfully.
在Outlook上可以看到收件箱已经有一封邮件: