abstract voidsetFrom(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 |
BCCThe "Bcc" (blind carbon copy) recipients. |
static Message.RecipientType |
CCThe "Cc" (carbon copy) recipients. |
static Message.RecipientType |
TOThe "To" (primary) recipients. |
回复人
void |
setReplyTo(Address[] addresses)Set the addresses to which replies should be directed. |
标题
abstract void |
setSubject(String subject) |
内容
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("java_mail_001@163.com"));
// 多个收件人
msg.setRecipients(RecipientType.TO, InternetAddress.parse("java_mail_002@163.com,java_mail_003@163.com"));
// 抄送人
msg.setRecipient(RecipientType.CC, new InternetAddress("java_mail_001@163.com"));
// 暗送人
msg.setRecipient(RecipientType.BCC, new InternetAddress("java_mail_004@163.com"));
// 主题
msg.setSubject("中文主题");
// HTML内容
msg.setContent("
你好啊
", "text/html;charset=utf-8");
// 连接邮件服务器、发送邮件、关闭连接,全干了
Transport.send(msg);
}
} 本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17909093,转载请注明。