spring封装javamail

使用spring发送邮件,主要还是在实现org.springframework.mail.javamail.JavaMailSender这个接口,在借助于IoC的情况下我们可以通过配置来实现.且gmail需要ssl验证

<bean id="mailSender"
          class="org.springframework.mail.javamail.JavaMailSenderImpl">
          <property name="host">
          <value>smtp.gmail.com</value>
         </property>
          <property name="javaMailProperties">
              <props>
              <prop key="mail.smtp.auth">true</prop>
               <prop key="mail.smtp.timeout">25000</prop>
               <prop key="mail.smtp.starttls.enable">true</prop>
               <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory </prop>
             <prop key="mail.smtp.socketFactory.fallback">false</prop>
             </props>
         </property>
         <property name="port">
         <value>465</value>
         </property>
        <property name="username">
         <value>[email protected]</value>
         </property>
        <property name="password">
         <value>yangfeng69</value>
        </property>
     </bean>

 <bean id="mailMessage"  class="org.springframework.mail.SimpleMailMessage">
  <property name="from" value="[email protected]"/>
  <property name="subject" value="竞价通知"/>
 </bean>

public class MailService  {

private MailSender mailSender;
private SimpleMailMessage message;

 SimpleMailMessage msg = new SimpleMailMessage(this.message);
        msg.setTo("email");
        msg.setText("text")
        try
  {
            mailSender.send(msg);
        }
        catch(MailException ex)
  {
            System.err.println(ex.getMessage());           
        }

}

你可能感兴趣的:(spring,.net,bean,IOC,Gmail)