基于commons Email version 1.3
主要类:
SimpleEmail - This class is used to send basic text based emails.
MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
ImageHtmlEmail - This class is used to send HTML formatted emails with inline images. It has all of the capabilities as HtmlEmail but transform all image references to inline images.
EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
还有一个jdk中的类DefaultAuthenticator,用与用户验证
应用示例:
SimpleEmail发送普通文本邮件:
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);//使用SSL加密发送
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");//使用addTo可以添加多个目的地
email.send();
使用MultiPartEmail发送带附件的邮件:
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
//路径可以为绝对或相对地址,也可以使用setURL
attachment.setPath("mypictures/john.jpg");
//附件处理方式,作为附件或内嵌为内容
attachment.setDisposition(EmailAttachment.ATTACHMENT);
// attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
attachment.setDescription("Picture of John");
attachment.setName("John");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
发送HtmlEmail:
// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test email with inline image");
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");//使用内嵌图片
// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
也可以使用如下相对路径形式使用ImageHtml:
// load your HTML email template
String htmlEmailTemplate = ....
// define you base URL to resolve relative resource locations
URL url = new URL("http://www.apache.org");
// create the email message
HtmlEmail email = new ImageHtmlEmail();
//指定html模板中的baseUrl
email.setDataSourceResolver(new DataSourceUrlResolverl(url));
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test email with inline image");
// set the html message
email.setHtmlMsg(htmlEmailTemplate);
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
补充几点:
1.开启email组件的调试,以便出现问题时能够sysout相关信息。
email.setDebug(true);
2.邮件安全:
开启STARTTLS or SSL检察:
Email.setSSLCheckServerIdentity(true)
开启:STARTTLS:Email.setStartTLSRequired(true)
开启SSL连接支持email.setSSLOnConnect(true);