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

来源:互联网 发布:台湾国民党解放军知乎 编辑:程序博客网 时间:2024/05/07 15:11
1.使用Apache-commons-email发送邮件非常简单,只需要加载三个jar包:commons-email-1.1.jarmail.jaractivition.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 thecapabilities 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 ofattachments. It is for use with instances of MultiPartEmail andHtmlEmail.
2.下面两个经过测试的代码:
//发送简单的文本
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
 
publicclass mailSender {
 /**
     *@测试成功!!!
     */
 publicstaticvoid 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;
 
publicclass AttachMailSender {
 
    /**
     *@paramargs
     *@throwsException
     */
    publicstaticvoid 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();
 
    }
 
}
 
原创粉丝点击