use jakarta-commons email

use jakarta-commons email

requires jar: commons-email-1.0.jar, mail.jar, activation.jar

1. send text mail
SimpleEmail email  =   new  SimpleEmail();
email.setHostName(
" 211.154.104.29 " );
email.setAuthentication(
" [email protected] " , password);
email.addTo(
" [email protected] " " Water Ye " );
email.setFrom(
" [email protected] " " Water Ye " );
email.setSubject(
" Test message " );
email.setMsg(
" This is a simple test of commons-email " );
email.send();
中文问题:
//  email.setMsg("测试邮件");
email.setCharset( " UTF-8 " );
email.setContent(
" 测试邮件 " " text/plain;charset=GBK " );
SimpleEmail封得太过简单, 看代码就知道了.

2. Sending email with attachments
//  Create the attachment
EmailAttachment attachment  =   new  EmailAttachment();
attachment.setPath(
" C:/mail/hello.groovy " );
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(
" hello.groovy " );
attachment.setName(
" hello.groovy " );

//  Create the email message
MultiPartEmail email  =   new  MultiPartEmail();
email.setHostName(
" 211.154.104.29 " );
email.setAuthentication(
" [email protected] " , password);
email.addTo(
" [email protected] " " Water Ye " );
email.setFrom(
" [email protected] " " Water Ye " );
email.setSubject(
" hello groovy " );
email.setMsg(
" groovy hello world " );

email.attach(attachment);   
//  add the attachment
email.send();    //  send the email

3. send html email
//  Create the email message
HtmlEmail email  =   new  HtmlEmail();
email.setHostName(
" 211.154.104.29 " );
email.setAuthentication(
" [email protected] " , password);
email.addTo(
" [email protected] " " Water Ye " );
email.setFrom(
" [email protected] " " Water Ye " );
email.setSubject(
" Test email with inline image " );

//  embed the image and get the content id
URL url  =   new  URL( " http://www.itorgan.com/images/index/top1.gif " );
String cid 
=  email.embed(url,  " Itorgan logo " );

email.setHtmlMsg(
" <html>The itorgan logo - <img src=\ " cid: "  + cid +  " \ " ></html> " );  //  set the html message

email.setTextMsg(
" Your email client does not support HTML messages " );  //  set the alternative message

email.send();


martin xus已写过, 就不发布到首页了

你可能感兴趣的:(use jakarta-commons email)