• 利用spring-mail模块发送带附件邮件dome


    本例为maven项目,直接撸代码吧。

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.wengen</groupId>
      <artifactId>mailTest</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>mailTest Maven Webapp</name>
      <url>http://maven.apache.org</url>
      
      <properties>
              <spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
            <spring.version>4.2.5.RELEASE</spring.version>
            <spring-core.version>4.2.5.RELEASE</spring-core.version>
      </properties>
      
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
         <!-- spring mail -->
    
            <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.4</version>
            </dependency>
            
             <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>1.1</version>
            </dependency>
        
        <!-- Spring Config -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-instrument</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-oxm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.amqp</groupId>
                <artifactId>spring-rabbit</artifactId>
                <version>${spring.amqp.version}</version>
            </dependency>
        
      </dependencies>
      <build>
        <finalName>mailTest</finalName>
      </build>
    </project>
    View Code

    MailMail.java

    package mailTest;
    
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.mail.MailParseException;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.messaging.MessagingException;
    
    import javax.mail.internet.MimeMessage;
    
    public class MailMail {
        private JavaMailSender mailSender;
        private SimpleMailMessage simpleMailMessage;
    
        public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
            this.simpleMailMessage = simpleMailMessage;
        }
    
        public void setMailSender(JavaMailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void sendMail(String dear, String content) {
    
            MimeMessage message = mailSender.createMimeMessage();
    
            try{
                MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
                helper.setFrom(simpleMailMessage.getFrom());
                helper.setTo(simpleMailMessage.getTo());
                helper.setSubject(simpleMailMessage.getSubject());
                helper.setText(String.format(
                        simpleMailMessage.getText(), dear, content));
                //使用本地log.txt作为附件
                FileSystemResource file = new FileSystemResource("D:\log.txt");
                helper.addAttachment(file.getFilename(), file);
    
            }catch (Exception e) {
                throw new MailParseException(e);
            }
            mailSender.send(message);
        }
    }
    View Code

    Spring-Mail.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <!-- 邮件发送传输协议 -->
            <property name="protocol" value="smtp" />
            <!-- 邮件发送服务器,本例使用QQ企业邮箱 -->
            <property name="host" value="smtp.exmail.qq.com" />
            <!-- 邮件发送端口 587-->
            <property name="port" value="587" />
            <!-- 登陆企业邮箱的用户名,用户自己注册 -->
            <property name="username" value="user@qq.com" />
            <!-- 登陆企业邮箱的密码,用户注册时候设置密码,本例用户名和密码都是假的 -->
            <property name="password" value="password" />
    
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtp.starttls.enable">true</prop>
                </props>
            </property>
        </bean>
    
        <bean id="mailMail" class="mailTest.MailMail">
            <property name="mailSender" ref="mailSender" />
            <property name="simpleMailMessage" ref="customeMailMessage" />
        </bean>
    
        <bean id="customeMailMessage"
              class="org.springframework.mail.SimpleMailMessage">
            <!-- 发送邮件地址 -->
            <property name="from" value="from@qq.com" />
            <!-- 接收邮件地址 -->
            <property name="to" value="to@qq.com" />
            <!-- 邮件主题 -->
            <property name="subject" value="Testing Subject" />
            <!-- 发送内容 -->
            <property name="text">
                <value>
                    <![CDATA[
                        Dear %s,
                        Mail Content : %s
                    ]]>
                </value>
            </property>
        </bean>
    
    </beans>
    View Code

    MailTest.java

    package mailTest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MailTest {
         public static void main( String[] args )
            {
                ApplicationContext context =
                        new ClassPathXmlApplicationContext("classpath:/Spring-Mail.xml");
    
                MailMail mm = (MailMail) context.getBean("mailMail");
                //发送时候写好主题和内容
                mm.sendMail("Yiibai", "This is text content");
    
            }
    }
    View Code

    非常简单。注意Spring-Mail.xml里面需要配置的地方。还有可能出现包冲突异常:

    此异常咱没有解决,所以新搞了个项目,实现本例。

    业精于勤 ==不想升职加薪的码农不是好码农==
  • 相关阅读:
    《整理的艺术》读后感
    就算神游 之二:行路 2
    VBA实现outlook自动发信 2
    使用Event的体会
    DevExpress Asp.net(9) ASPxHiddenField的特性与基本使用
    .net 启动窗休的设计总结
    DevExpress Asp.net(5) ASPxCloudControl的基本使用
    DevExpress Asp.net(7) ASPxTreeList的基本使用之一
    数据结构树形结构(1)
    数据结构树形结构(2)
  • 原文地址:https://www.cnblogs.com/coreyjk/p/6733524.html
Copyright © 2020-2023  润新知