Spring 代理 JavaMailSenderImpl 服务器发送邮件


我用maven:重要的项目:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
 </dependency>
 
 <!-- java mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.2</version>
        </dependency>


配置org.springframework.mail.javamail.JavaMailSenderImpl:

<!-- Spring 代理 Javamail,这里使用网易邮箱 -->
    <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>smtp.163.com</value>
        </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>[email protected]</value>
        </property>
        <property name="password">
            <value>zhi19940306</value>
        </property>
    </bean>


把org.springframework.mail.javamail.JavaMailSenderImpl注入其他bean并且调用以下方法:

/**
     * Spring 代理 Javamail 发送邮件
     * @param email
     * @throws MessagingException
     */
    private void sendMail(String email) throws MessagingException {  

          JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  

          MimeMessage mailMessage = senderImpl.createMimeMessage();  

           //设置utf-8或GBK编码,否则邮件会有乱码  

          MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");  

         try {  

           messageHelper.setTo(email);//接受者     

           messageHelper.setFrom("[email protected]");//发送者  

           messageHelper.setSubject("欢迎加入 JAVAEYE™ 社区");//主题  

           //邮件内容,注意加参数true,表示启用html格式 
           
           String emailContent = "<html><head></head><body><h1>您已经注册!!</h1>"
                       + "<h2>点击以下超链接激活账户以完成注册</h2><a href='http://localhost:8080/JavaEye/register_active?id="+user.getId()+"'>点击该超链接激活您的账户</a><br/>"
                               + "<span>温馨提示:请勿回复本邮件</span></body></html>";

           messageHelper.setText(emailContent,true);  

           sender.send(mailMessage);  
           
           /**
            * 获取邮箱类型
            */
           String emailType = user.getEmail().substring(user.getEmail().indexOf("@") + 1, user.getEmail().indexOf("."));
           
           ActionContext actionContext = ActionContext.getContext();
           
           Map<String, Object> session = actionContext.getSession();
           
           session.put("emailType", emailType);
           
          } catch (Exception e) {  

           e.printStackTrace();  

          }  
    }


你可能感兴趣的:(Spring 代理 JavaMailSenderImpl 服务器发送邮件)