本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17909093,转载请注明。
上篇文章介绍了JavaMail并实现了一个发送邮件的简单示例,JavaMail API使用上非常灵活,比如,服务器信息可以设置到Session中,也可以设置到Transport中,收件人可以设置到Message中,也可以设置到Transport中,如何使用,取决于我们应用程序中的实际情况。本文详细的介绍一下这三个类的主要方法。
static Session |
getDefaultInstance(Properties props) Get the default Session object. |
static Session |
getDefaultInstance(Properties props,Authenticator authenticator) Get the default Session object. |
static Session |
getInstance(Properties props) Get a new Session object. |
static Session |
getInstance(Properties props,Authenticator authenticator) Get a new Session object. |
# JavaMail IMAP provider Sun Microsystems, Inc protocol=imap; type=store; class=com.sun.mail.imap.IMAPStore; vendor=Sun Microsystems, Inc; protocol=imaps; type=store; class=com.sun.mail.imap.IMAPSSLStore; vendor=Sun Microsystems, Inc; # JavaMail SMTP provider Sun Microsystems, Inc protocol=smtp; type=transport; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc; protocol=smtps; type=transport; class=com.sun.mail.smtp.SMTPSSLTransport; vendor=Sun Microsystems, Inc; # JavaMail POP3 provider Sun Microsystems, Inc protocol=pop3; type=store; class=com.sun.mail.pop3.POP3Store; vendor=Sun Microsystems, Inc; protocol=pop3s; type=store; class=com.sun.mail.pop3.POP3SSLStore; vendor=Sun Microsystems, Inc;每一行声明了协议名称、类型、实现类、供应商、版本等信息,如果需要自己实现相应的协议,必须按照该格式配置好,Java Mail API中才能正确的调用到。Session中提供的创建Trasnsport和Store的方法如下:
Store |
getStore() Get a Store object that implements this user's desired Store protocol. |
Store |
getStore(Provider provider) Get an instance of the store specified by Provider. |
Store |
getStore(String protocol) Get a Store object that implements the specified protocol. |
Store |
getStore(URLName url) Get a Store object for the given URLName. |
Transport |
getTransport() Get a Transport object that implements this user's desired Transport protcol. |
Transport |
getTransport(Address address) Get a Transport object that can transport a Message of the specified address type. |
Transport |
getTransport(Provider provider) Get an instance of the transport specified in the Provider. |
Transport |
getTransport(String protocol) Get a Transport object that implements the specified protocol. |
Transport |
getTransport(URLName url) Get a Transport object for the given URLName. |
abstract void |
setFrom() Set the "From" attribute in this Message. |
abstract void |
setFrom(Address address) Set the "From" attribute in this Message. |
void |
setRecipient(Message.RecipientType type,Address address) Set the recipient address. |
abstract void |
setRecipients(Message.RecipientType type,Address[] addresses) Set the recipient addresses. |
static Message.RecipientType |
BCC The "Bcc" (blind carbon copy) recipients. |
static Message.RecipientType |
CC The "Cc" (carbon copy) recipients. |
static Message.RecipientType |
TO The "To" (primary) recipients. |
void |
setReplyTo(Address[] addresses) Set the addresses to which replies should be directed. |
abstract void |
setSubject(String subject) Set the subject of this message. |
void |
setContent(Multipart mp) This method sets the given Multipart object as this message's content. |
void |
setContent(Object obj,String type) A convenience method for setting this part's content. |
void |
setText(String text) A convenience method that sets the given String as this part's content with a MIME type of "text/plain". |
public class JavaMailTest2 { public static void main(String[] args) throws MessagingException { Properties props = new Properties(); // 开启debug调试 props.setProperty("mail.debug", "true"); // 发送服务器需要身份验证 props.setProperty("mail.smtp.auth", "true"); // 设置邮件服务器主机名 props.setProperty("mail.host", "smtp.163.com"); // 发送邮件协议名称 props.setProperty("mail.transport.protocol", "smtp"); // 设置环境信息 Session session = Session.getInstance(props, new Authenticator() { // 在session中设置账户信息,Transport发送邮件时会使用 protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("java_mail_001", "javamail"); } }); // 创建邮件对象 Message msg = new MimeMessage(session); // 发件人 msg.setFrom(new InternetAddress("[email protected]")); // 多个收件人 msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]")); // 抄送人 msg.setRecipient(RecipientType.CC, new InternetAddress("[email protected]")); // 暗送人 msg.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]")); // 主题 msg.setSubject("中文主题"); // HTML内容 msg.setContent("<div align=\"center\">你好啊</div>", "text/html;charset=utf-8"); // 连接邮件服务器、发送邮件、关闭连接,全干了 Transport.send(msg); } }本文来自: 高爽|Coder,原文地址: http://blog.csdn.net/ghsau/article/details/17909093,转载请注明。