本文分别测试使用java mail api(http://www.oracle.com/technetwork/java/javamail/index.html)和 apache mail api(http://commons.apache.org/email/)发送邮件的功能。(其中要注意 apache mail 依赖于 java mail 。)测试代码如下:
import java.net.URL; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.Email; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; import org.junit.After; import org.junit.Before; import org.junit.Test; public class JavaMail { @Test public void defaultSendMail() throws Exception { String host = "smtp.sina.com";// "smtp.gmail.com" String port = "25"; //"465" Properties props = new Properties(); props.setProperty("mail.smtp.host", host); props.setProperty("mail.smtp.port", port); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.ssl.enable", "false");//"true" props.setProperty("mail.smtp.connectiontimeout", "5000"); final String user = "***@sina.com"; //"***@gmail.com" final String pwd = "***"; Session session = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { //登录用户名密码 return new PasswordAuthentication(user,pwd); } }); session.setDebug(true); Transport transport = session.getTransport("smtp");//"smtps" transport.connect(host,user,pwd); //消息 MimeMessage message = new MimeMessage(session); setMailContent(message);//。。。。。。。。。。。。。。。。。。 message.setSubject("邮件标题"); //消息发送者接收者设置 //message.setFrom(new InternetAddress(user,"发件人名称")); message.addRecipients(Message.RecipientType.TO,new InternetAddress[]{ new InternetAddress("[email protected]","to1昵称"), new InternetAddress("[email protected]","to2昵称") }); message.saveChanges(); //发送 transport.send(message); //Transport.send(message); transport.close(); } //设定邮件内容 private void setMailContent(MimeMessage message) throws MessagingException { //方法一:只有文本邮件 //message.setContent("邮件内容..", "text/plain");//纯文本内容 //方法二:添加附件的邮件 Multipart part = new MimeMultipart(); BodyPart bodypart1 = new MimeBodyPart(); bodypart1.setText("邮件内容"); part.addBodyPart(bodypart1 ); BodyPart bodypart2 = new MimeBodyPart(); bodypart2.setFileName("fileName"); bodypart2.setDataHandler(new DataHandler(new FileDataSource("f:\\ok.txt"))); part.addBodyPart(bodypart2); message.setContent(part); } @Test public void apacheSendMail() throws Exception { SimpleEmail email = new SimpleEmail(); email.setHostName("smtp.gmail.com"); email.setSSL(true); email.setSmtpPort(465); // email.setSslSmtpPort("465"); email.setTLS(true);//gmail email.setAuthenticator(new DefaultAuthenticator("h***@gmail.com", "***")); email.setFrom("h***@gmail.com"); email.setSubject("TestCommonMail"); email.setCharset("gbk"); //文本邮件 email.setMsg("This is a test mail ... :-)"); email.addTo("**[email protected]"); email.send(); } @Test public void apacheSendHtmlMail() throws Exception { HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.gmail.com"); email.setSSL(true); email.setSmtpPort(465); email.setTLS(true);//gmail email.setAuthenticator(new DefaultAuthenticator("h***@gmail.com", "***")); email.setFrom("h***@gmail.com"); email.addTo("**[email protected]"); email.setSubject("TestCommonMail"); email.setCharset("gbk"); //html邮件 String cid = email.embed(new URL("http://www.google.com.tw/intl/en_com/images/srpr/logo1w.png"), "google logo"); email.setHtmlMsg("<html>The logo - <img src='cid:"+cid+"'></html>"); email.send(); } @Test public void apacheSendAttachMail() throws Exception { MultiPartEmail email = new MultiPartEmail(); email.setHostName("smtp.gmail.com"); email.setSSL(true); email.setSmtpPort(465); email.setTLS(true);//gmail email.setAuthenticator(new DefaultAuthenticator("h***@gmail.com", "***")); email.setFrom("h***@gmail.com"); email.addTo("**[email protected]"); email.setSubject("TestCommonMail"); email.setMsg("This is a test mail ... :-)"); email.setCharset("gbk"); EmailAttachment attach = new EmailAttachment(); attach.setName("attachFileName"); attach.setPath("f:\\ok.txt"); attach.setDescription(EmailAttachment.ATTACHMENT); email.attach(attach ); email.send(); } }