`
sjuhui
  • 浏览: 46058 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring send mail

阅读更多

使用Spring发邮件

关于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 {
 /**
  * @param args
  */
 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("邮件发送成功.....");
 }
    }
分享到:
评论

相关推荐

    spring-boot-mail

    spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail.port=25 #协议 spring.mail.protocol=smtp #邮箱账号名 spring.mail.username=Email_Name #Spring...

    thymeleafexamples-springmail-3.0-master.zip

    This is an example application showing how to compose and send dynamic e-mails using Spring and Thymeleaf. With Thymeleaf you can compose text and HTML emails easily. To learn more about Thymeleaf ...

    Learning Spring Application Development

    With this practical guide, you will learn best ... securing your applications against malicious intruders, and exploring the Spring Mail Application Programming interface to send and receive e-mails.

    Learning.Spring.Application.Development.1783987367

    securing your applications against malicious intruders, and exploring the Spring Mail Application Programming interface to send and receive e-mails. Table of Contents Chapter 1: Introducing The ...

    Java Mail封装的Jar包

    封装了一个java mail的jar包,只需设置邮件的基本信息 就可发邮件: //这个类主要是设置邮件 MailSenderInfo mailInfo = new MailSenderInfo(); ... ...//sms.sendHtmlMail(mailInfo);//发送html格式

    Learning Spring Application Development(PACKT,2015)

    With this practical guide, you will learn best ... securing your applications against malicious intruders, and exploring the Spring Mail Application Programming interface to send and receive e-mails.

    spring-boot-邮件发送

    Spring Boot通过集成spring-boot-starter-mail依赖和相关配置,可以方便地实现邮件发送功能。具体的步骤: 添加依赖:在Spring Boot项目的pom.xml文件中加入spring-boot-starter-mail依赖,如果需要发送模板邮件,还...

    Spring-Boot-Send-Email-with-AWS-SES:Spring Boot使用AWS SES发送电子邮件

    Spring启动与AWS-SES发送电子邮件

    spring-boot-send-mail-via-SMTP:使用Spring Boot通过SMT发送邮件

    春天引导开发春天引导发展

    python-send-mail:从 Windows 机器通过 smtplib 发送电子邮件的 python 脚本

    这个脚本是用 Python 2.7.9 和 pip 1.5.6 开发和测试的范围python-send-mail 是 Lukas Ziegler 在 2015 年Spring为他的硕士论文创建的。之所以选择这种方法,是因为周围框架给出了要实现发送邮件功能的条件。 规范...

    freemarker总结

    Email: ${mail} &lt;/#macro&gt; &lt;#assign mail = "jsmith@acme.com"&gt; ? 使用import指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量: ...

Global site tag (gtag.js) - Google Analytics