java 发送邮件测试例子

首先 需要两个jar,分别是mail.jar和 spring.jar

在工程中,找到applicationContext.xml 文件,将下面一段拷进去,

<!– 配置spring 发邮件 –>
 <bean id=”javaMailSender” class=”org.springframework.mail.javamail.JavaMailSenderImpl”>  

  <property name=”host” value=”smtp.qq.com” />  <!– 邮箱发件服务器 –>

 <property name=”username” value=”邮箱登陆名” /> 
    <property name=”password” value=”邮箱登陆密码” /> 
    <property name=”javaMailProperties”> 
        <props> 
            <prop key=”mail.smtp.auth”>true</prop> 
            <prop key=”mail.smtp.timeout”>25000</prop>  
              </props> 
    </property> 
 </bean> 

然后写一个测试类,如下

public class SendMail {  
    public static void main(String[] args) throws Exception {  
        ApplicationContext ctx = new FileSystemXmlApplicationContext(  
                “src/local/applicationContext.xml”);  
        JavaMailSender sender = (JavaMailSender) ctx.getBean(“javaMailSender”);  

      
        SendMail springMail = new SendMail();  
 
        //测试发送只有文本信息的简单测试  
        System.out.println(“开始发送”);
        springMail.sendTextMail(sender); 
        System.out.println(“发送成功”);
        //测试发送html  
//        springMail.sendHtml(sender);  
        //测试发送带附件的邮件  
//        springMail.sendMimeMessage(sender);  
    }  

    /** *//** 
     * 测试发送只有文本信息的简单测试 
     * @param sender 邮件发送器 
     * @throws Exception 
     */ 
    private void sendTextMail(JavaMailSender sender) throws Exception {  
        SimpleMailMessage mail = new SimpleMailMessage(); 
        System.out.print(“ddd”);  
        mail.setTo(“wagfei@wondersgroup.com“);  

        mail.setFrom(“2604@qq.com“);  

        mail.setSubject(“test by amigo”);  
        mail.setText(“spring Mail的简单测试”);  
        sender.send(mail);  
       
        System.out.println(“成功发送文本文件!”);  
    }  
    /** *//** 
     * 测试发送有html的简单测试 
     * @param sender 邮件发送器 
     * @throws Exception 
     */ 
    private void sendHtml(JavaMailSender sender)throws Exception{  
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();   
        MimeMessage mailMessage = senderImpl.createMimeMessage();   
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,”UTF-8″);   

                       
        //设置收件人,寄件人   
        messageHelper.setTo(“wangfei@wondersgroup.com“);   

        messageHelper.setFrom(“caoei@wondersgroup.com“);   

        messageHelper.setSubject(“测试HTML邮件!”);   
        //true 表示启动HTML格式的邮件   
        messageHelper.setText(“<html><head></head><body><h1>hello!!王朋飞</h1></body></html>”,true);    

        //发送邮件   
        sender.send(mailMessage);  
        System.out.println(“html发送成功…..”);   
    }  
    /** *//** 
     * 测试发送有html和附件的简单测试 
     * @param sender 邮件发送器 
     * @throws Exception 
     */ 
    private boolean sendMimeMessage(JavaMailSender sender)throws Exception{  
        try {
   JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();   
   MimeMessage mailMessage = senderImpl.createMimeMessage();   
   //如果是增加附件mimeMessageHelper后一个参数要为true ,最后一个参数是设置编码
   MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,”UTF-8″);   

   //设置收件人,寄件人   
   
//         String[] array = new String[]{“wgfei@wondersgroup.com”,”wumgni@wondersgroup.com”,”yinng@wondersgroup.com”,”dg@wondersgroup.com“};   

//   String[] array = new String[]{“wagfei@wondersgroup.com”,”sdfsafsdfsfsfsadsf@wondersgroup.com“};   

//   messageHelper.setTo(array);   
   messageHelper.setTo(“wani@wondersgroup.com“);   

   messageHelper.setFrom(“caoei@wondersgroup.com“);   

   messageHelper.setSubject(“测试群发带附件邮件和乱码!”);   
   //true 表示启动HTML格式的邮件   
   messageHelper.setText(“<html><head></head><body><h1><a href=’www.baidu.com’>www.baidu.com</a>hello!!大家好,这是一封群发测试邮件。</h1></body></html>”,true);   

   //附件内容  
   FileSystemResource file = new FileSystemResource(new File(“d:/top.html”));     

   // 这里的方法调用和插入图片是不同的。     
   messageHelper.addAttachment(“top.html”, file);     
   //发送邮件   
   sender.send(mailMessage);
   System.out.println(“附件发送成功…..”);
   return true;   
  } catch (MessagingException ex) {
   ex.printStackTrace();   
   return false;   
  }  
    }  

 

« »

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *