2015-03-06 11:57

Spring JavaMail 筆記

Gmail via SSL
  1. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
  2.    <property name="host" value="smtp.gmail.com" /> 
  3.    <property name="port" value="465" /> 
  4.    <property name="username" value="smtp-user" /> 
  5.    <property name="password" value="smtp-passwd" /> 
  6.  
  7.    <property name="javaMailProperties"> 
  8.        <props> 
  9.            <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop> 
  10.            <prop key="mail.smtp.auth">true</prop> 
  11.        </props> 
  12.    </property> 
  13. </bean> 


Gmail via TLS
  1. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
  2.    <property name="host" value="smtp.gmail.com" /> 
  3.    <property name="port" value="587" /> 
  4.    <property name="username" value="smtp-user" /> 
  5.    <property name="password" value="smtp-passwd" /> 
  6.    <property name="javaMailProperties"> 
  7.        <props> 
  8.            <prop key="mail.smtp.starttls.enable">true</prop> 
  9.            <prop key="mail.smtp.auth">true</prop> 
  10.        </props> 
  11.    </property> 
  12. </bean> 


Sample Code
  1. package test_mail; 
  2.  
  3. import javax.mail.Message; 
  4. import javax.mail.MessagingException; 
  5. import javax.mail.internet.MimeMessage; 
  6.  
  7. import org.springframework.context.support.AbstractApplicationContext; 
  8. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  9. import org.springframework.mail.SimpleMailMessage; 
  10. import org.springframework.mail.javamail.JavaMailSender; 
  11. import org.springframework.mail.javamail.MimeMessageHelper; 
  12.  
  13. public class TestSpringMail { 
  14.  
  15.    public static void main( String[] args ) throws Exception { 
  16.  
  17.        AbstractApplicationContext context 
  18.            = new ClassPathXmlApplicationContext("test_mail/spring-mail.xml"); 
  19.  
  20.        JavaMailSender mailSender  
  21.            = (JavaMailSender) context.getBean("mailSender"); 
  22.  
  23.        sample1(mailSender); 
  24.        sample2(mailSender); 
  25.        sample3(mailSender); 
  26.  
  27.        context.close(); 
  28.    } 
  29.  
  30.  
  31.    public static void sample1(JavaMailSender mailSender) throws Exception { 
  32.        MimeMessage mimeMessage = mailSender.createMimeMessage(); 
  33.  
  34.        mimeMessage.setFrom("from@no-spam.com"); 
  35.        mimeMessage.setRecipients( 
  36.            Message.RecipientType.TO, "to@no-spam.com" 
  37.        ); 
  38.        mimeMessage.setSubject("Testing Subject"); 
  39.        mimeMessage.setContent( 
  40.            "<b>Testing Content.</b>",  
  41.            "text/html; charset=utf-8" 
  42.        ); 
  43.  
  44.        mailSender.send(mimeMessage); 
  45.    } 
  46.  
  47.  
  48.    public static void sample2(JavaMailSender mailSender) throws Exception { 
  49.        MimeMessage mimeMessage = mailSender.createMimeMessage(); 
  50.        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8"); 
  51.  
  52.        message.setFrom("from@no-spam.com"); 
  53.        message.setTo("to@no-spam.com"); 
  54.        message.setSubject("Testing Subject"); 
  55.        message.setText("<b>Testing Content.</b>", true); 
  56.  
  57.        mailSender.send(mimeMessage); 
  58.    } 
  59.  
  60.  
  61.    public static void sample3(JavaMailSender mailSender) { 
  62.        SimpleMailMessage message = new SimpleMailMessage(); 
  63.        message.setFrom("from@no-spam.com"); 
  64.        message.setTo("to@no-spam.com"); 
  65.        message.setSubject("Testing Subject"); 
  66.        message.setText("Testing Content."); 
  67.  
  68.        mailSender.send(message); 
  69.    } 
  70.  
  71. } 


參考自:
JavaMail API – Sending email via Gmail SMTP example : Mkyong
Spring – Sending e-mail with attachment : Mkyong
Spring – Define an E-mail template in bean configuration file : Mkyong
Spring – Sending E-mail via Gmail SMTP server with MailSender : Mkyong

0 回應: