zoukankan      html  css  js  c++  java
  • 使用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 + "。");

  • 相关阅读:
    CentOS 8配置Java环境
    记录一个免费的开源API接口管理工具
    WebAPI 查询lookup字段的属性
    Windows环境变量配置与读取
    The specified Active Directory user already exists as a Dynamics 365 user
    QueryExpression之GreaterEqual和LessEqual
    Dynamics CRM Plugin Use Config
    【转】Reports SPN/SSPI Problems
    【转】Report Server cannot load the TERADATA / SQLPDW extension
    iOS 自动布局
  • 原文地址:https://www.cnblogs.com/esther-qing/p/6288506.html
Copyright © 2011-2022 走看看