使用Spring发邮件

用到的jar  spring-context.jar
  mail.jar
  spring-core.jar
  activation.jar
  commons-logging.jar

     

1:简单邮件
  简单邮件中是可以实现邮件群发的功能的,区别是收件人可以是一个数组。

 

     package com; 
     import org.springframework.mail.SimpleMailMessage; 
     import org.springframework.mail.javamail.JavaMailSenderImpl; 
        /** 
            * 本类测试简单邮件 
            * @author sunny 
            * 
            */
 
   public class SingleMailSend { 
         public static void main(String args[]){ 
         JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 
          //设定mail server 
         senderImpl.setHost("192.168.1.1"); 
         senderImpl.setUsername("username"); 
         senderImpl.setPassword("password"); 
     
     
         //建立邮件消息 
         SimpleMailMessage mailMessage = new SimpleMailMessage(); 
         //设置收件人,寄件人 
         //String[] array = new String[]    {"sun111@163.com","sun222@sohu.com"};    
        //mailMessage.setTo(array); 
        mailMessage.setTo("sun111@163.com"); 
         mailMessage.setFrom("webadmin@163.com"); 
         mailMessage.setSubject("测试邮件群发!"); 
         mailMessage.setText("测试我的简单邮件发送机制!!"); 
     
         //发送邮件 
         senderImpl.send(mailMessage); 
     
        System.out.println("邮件发送成功....."); 
     } 
 }

 

 

2:HTML邮件的发送

       package com; 
        import javax.mail.internet.MimeMessage; 
        import org.springframework.mail.javamail.JavaMailSenderImpl; 
        import org.springframework.mail.javamail.MimeMessageHelper; 
        /** 
         * 本类测试html邮件 
         * @author sunny 
         * 
         */
 
        public class HTMLMailDemo { 
             public static void main(String[] args) throws Exception{ 
                   JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 
     
                        //设定mail server 
                       senderImpl.setHost("192.168.1.1"); 
                       senderImpl.setUsername("username"); 
                       senderImpl.setPassword("password"); 
     
                      //建立邮件消息,发送简单邮件和html邮件的区别 
                       MimeMessage mailMessage = senderImpl.createMimeMessage(); 
                       MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); 
                 
                      //设置收件人,寄件人 
                       messageHelper.setTo("sun111@163.com"); 
                       messageHelper.setFrom("webadmin@163.com"); 
                       messageHelper.setSubject("测试HTML邮件!"); 
                       //true 表示启动HTML格式的邮件 
                      messageHelper.setText("<html><head></head><body><h1>hello!!zhangjian</h1></body></html>",true); 
     
                       //发送邮件 
                      senderImpl.send(mailMessage); 
     
                      System.out.println("邮件发送成功....."); 
              } 
 }

 

 

3:本类测试邮件中嵌套图片

        package com; 
        import java.io.File; 
        import javax.mail.internet.MimeMessage; 
        import org.springframework.core.io.FileSystemResource; 
        import org.springframework.mail.javamail.JavaMailSenderImpl; 
        import org.springframework.mail.javamail.MimeMessageHelper; 
        /** 
         * 本类测试邮件中嵌套图片 
         * @author sunny    
         * 
         */
 
        public class AttachedImageMail { 
                public static void main(String[] args) throws Exception{ 
                     JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 
     
                     //设定mail server 
                     senderImpl.setHost("192.168.1.2"); 
                     senderImpl.setUsername("username"); 
                     senderImpl.setPassword("pssword"); 
                     //建立邮件消息,发送简单邮件和html邮件的区别 
                     MimeMessage mailMessage = senderImpl.createMimeMessage(); 
                     //注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,         
                  //multipart模式 
                   MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); 
     
                   //设置收件人,寄件人 
                    messageHelper.setTo("sun111@163.com"); 
                     messageHelper.setFrom("webadmin@163.com"); 
                    messageHelper.setSubject("测试邮件中嵌套图片!!"); 
                    //true 表示启动HTML格式的邮件 
                     messageHelper.setText("<html><head></head><body><h1>hello!!zhangjian</h1>" + 
                      "<img src=\"cid:aaa\"/></body></html>",true); 
            
                    FileSystemResource img = new FileSystemResource(new File("c:/aaa.jpg")); 
     
                   messageHelper.addInline("aaa",img); 
     
                    //发送邮件 
                   senderImpl.send(mailMessage); 
     
                   System.out.println("邮件发送成功....."); 
           } 
   } 

 

 

 

4:  测试邮件中包含附件

 

    package com; 
    
        import java.io.File; 
        import javax.mail.internet.MimeMessage; 
        import org.springframework.core.io.FileSystemResource; 
        import org.springframework.mail.javamail.JavaMailSenderImpl; 
        import org.springframework.mail .javamail.MimeMessageHelper; 
        public class AttachedFileMail { 

         /** 
          * 本类测试的是关于邮件中带有附件的例子 
          * @param args 
          */
 

   public static void main(String[] args) throws Exception{ 
          JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 
     
          //设定mail server 
            senderImpl.setHost("192.168.1.2"); 
            senderImpl.setUsername("username"); 
            senderImpl.setPassword("password"); 
           //建立邮件消息,发送简单邮件和html邮件的区别 
           MimeMessage mailMessage = senderImpl.createMimeMessage(); 
          //注意这里的boolean,等于真的时候才能嵌套图片,在构建MimeMessageHelper时候,所给定的值是true表示启用,         
         //multipart模式 
          MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); 
     
           //设置收件人,寄件人 
            messageHelper.setTo("sun111@163.com");    
            messageHelper.setFrom("webadmin@163.com"); 
           messageHelper.setSubject("测试邮件中上传附件!!"); 
           //true 表示启动HTML格式的邮件 
           messageHelper.setText("<html><head></head><body><h1>你好:附件中有关于CSS的学习资料!!         
           </h1></body></html>",true); 
            
           FileSystemResource file = new FileSystemResource(new File("d:/CSS110.rar")); 
            //这里的方法调用和插入图片是不同的。 
            messageHelper.addAttachment("CSS110.rar",file); 
     
           //发送邮件 
          senderImpl.send(mailMessage); 
     
          System.out.println("邮件发送成功....."); 
     } 
 }

posted @ 2012-03-22 16:30  张良  阅读(674)  评论(0编辑  收藏  举报