使用java mail api 和 apache mail api 发送email 代码示例
本文分别测试使用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 { @Testpublic 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() {@Overrideprotected 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("to1@163.com","to1昵称"),new InternetAddress("to2@163.com","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);}@Testpublic 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);//gmailemail.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("**to@qq.com");email.send(); }@Testpublic void apacheSendHtmlMail() throws Exception {HtmlEmail email = new HtmlEmail();email.setHostName("smtp.gmail.com");email.setSSL(true);email.setSmtpPort(465); email.setTLS(true);//gmailemail.setAuthenticator(new DefaultAuthenticator("h***@gmail.com", "***"));email.setFrom("h***@gmail.com");email.addTo("**to@qq.com");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(); } @Testpublic void apacheSendAttachMail() throws Exception {MultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.gmail.com");email.setSSL(true);email.setSmtpPort(465); email.setTLS(true);//gmailemail.setAuthenticator(new DefaultAuthenticator("h***@gmail.com", "***"));email.setFrom("h***@gmail.com");email.addTo("**to@qq.com");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(); } }