使用Apache email组件发送邮件实例

 
1 .使用 Apache-commons-email 发送邮件非常简单 , 只需要加载三个 jar 包: commons-email-1.1.jar mail.jar activition.jar
 
Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Some of the mail classes that are provided are as follows:
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.
EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
2.下面两个经过测试的代码:
//发送简单的文本
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
 
public class mailSender {
  /**
      * @ 测试成功!!!
      */
  public static void main(String[] args) {
  // 不要使用 SimpleEmail, 会出现乱码问题
   HtmlEmail email = new HtmlEmail();
   try {
      // 这里是发送服务器的名字
       email.setHostName( "smtp.sohu.com" );
       // 编码集的设置
       email.setCharset( "gbk" );
       // 收件人的邮箱
       email.addTo( "pengchua@gmail.com" );
       // 发送人的邮箱
       email.setFrom( "ppzhguy@sohu.com" , "ppzhguy" );
       // 如果需要认证信息的话,设置认证:用户名 - 密码。分别为发件人在邮件服务器上的注册名称和密码
       email.setAuthentication( "ppzhguy" , "XXXX" );
       email.setSubject( " 测试 Email" );
       // 要发送的信息
        email.setMsg( " 测试 Email " );
        // 发送
        email.send();
 } catch (EmailException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
 }
 }
} }
//发送附件:
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
 
public class AttachMailSender {
 
    /**
      * @param args
      * @throws Exception
      */
    public static void main(String[] args) throws Exception {
       // TODO Auto-generated method stub
//     Create the attachment
        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath( "D:/ 测试 .jpg" ); // 指定附件在本地的绝对路径
        attachment.setDisposition(EmailAttachment. ATTACHMENT );
        attachment.setDescription( "Picture of test" ); // 附件描述
//      attachment.setName(" 测试 ");// 附件名称
         // 如果附件是中文名会在乱码 ,attachment.setName(MimeUtility.encodeText(" 测试 "));
        attachment.setName(MimeUtility.encodeText( " 测试 " ));
         // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setHostName( "smtp.sohu.com" );
           // 编码集的设置
          email.setCharset( "gbk" );
           // 收件人的邮箱
          email.addTo( "pengchua@gmail.com" );
           // 发送人的邮箱
          email.setFrom( "ppzhguy@sohu.com" , "ppzhguy" );
           // 如果需要认证信息的话,设置认证:用户名 - 密码。分别为发件人在邮件服务器上的注册名称和密码
          email.setAuthentication( "ppzhguy" , "XXXXX" );
        email.setSubject( " 图片 " );
        email.setMsg( " 这是你想要的图片 !" );
        
         // add the attachment
        email.attach(attachment);
        
         // send the email
        email.send();
 
    }
 
}
 
 
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值