美文网首页
使用SpringMVC+Java mail发送HTML邮件

使用SpringMVC+Java mail发送HTML邮件

作者: Kjiang | 来源:发表于2015-11-29 19:36 被阅读2534次

    1 添加基本依赖

    JavaMail

    • mail.jar

    Spring framework

    • spring-beans-3.2.0.RELEASE.jar
    • spring-context-3.2.0.RELEASE.jar
    • spring-context-support-3.2.0.RELEASE.jar
    • spring-core-3.2.0.RELEASE.jar
    • spring-expression-3.2.0.RELEASE.jar
    • spring-web-3.2.0.RELEASE.jar
    • spring-webmvc-3.2.0.RELEASE.jar

    Apache Commons Logging

    • commons-logging-1.1.1.jar

    2 添加xml配置到spring配置文件中

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
         <property name="host" value="smtp.gmail.com" />
         <property name="port" value="587" />
         <property name="username" value="你的email地址" />
         <property name="password" value="密码" />
         <property name="javaMailProperties">
               <props>
                  <prop key="mail.transport.protocol">smtp</prop>
                  <prop key="mail.smtp.auth">true</prop>
                  <prop key="mail.smtp.starttls.enable">true</prop>
              </props>
    </property>
    </bean>
    

    3 发送html格式邮件

    MimeMessage mimeMsg = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, "utf-8");
    mimeMsg.setContent(htmlContent, "text/html");
    helper.setTo(recipientAddress);
    helper.setSubject(subject);
    helper.setFrom(Constant.SITE_EMAIL);
    mailSender.send(mimeMsg);
    

    相关文章

      网友评论

          本文标题:使用SpringMVC+Java mail发送HTML邮件

          本文链接:https://www.haomeiwen.com/subject/mvqjhttx.html