使用spring的JavaMailSender发送邮件


步骤:
    一:pom.xml

      <!-- java邮件 -->
      <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4.7</version>
      </dependency>

二:mail.properties

mail.host=smtp.hsmdata.com
mail.username=**@hsmdata.com
mail.password=**

三:applicationContext.xml

<!--发送邮件-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
        <property name="username" value="${mail.username}"></property>
        <property name="password" value="${mail.password}"></property>
    </bean>

四: 使用   

 @Autowired
    private JavaMailSender mailSender;

    public void toEmail(SimpleMailMessage mail){
        mailSender.send(mail);
    }

            String str = String.valueOf(Math.round(Math.random() * 1000000));
            SimpleMailMessage mail = new SimpleMailMessage();
            mail.setTo(email);
            mail.setFrom(PropertiesUtil.getProperty(PropertiesConstant.MAIL_USERNAME));
            mail.setSubject("价值连城项目验证邮件");
            mail.setText("尊敬的用户:您好,感谢您使用价值连城项目,您的验证码为" + str + "。");

 原文链接:https://www.cnblogs.com/esther-qing/p/6288506.html

猜你喜欢

转载自blog.csdn.net/qq3892997/article/details/82588326
今日推荐