- <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host" value="smtp.gmail.com" />
- <property name="port" value="465" />
- <property name="username" value="smtp-user" />
- <property name="password" value="smtp-passwd" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
Gmail via TLS
- <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="smtp-user" />
- <property name="password" value="smtp-passwd" />
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.starttls.enable">true</prop>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
Sample Code
- package test_mail;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.MimeMessageHelper;
- public class TestSpringMail {
- public static void main( String[] args ) throws Exception {
- AbstractApplicationContext context
- = new ClassPathXmlApplicationContext("test_mail/spring-mail.xml");
- JavaMailSender mailSender
- = (JavaMailSender) context.getBean("mailSender");
- sample1(mailSender);
- sample2(mailSender);
- sample3(mailSender);
- context.close();
- }
- public static void sample1(JavaMailSender mailSender) throws Exception {
- MimeMessage mimeMessage = mailSender.createMimeMessage();
- mimeMessage.setFrom("from@no-spam.com");
- mimeMessage.setRecipients(
- Message.RecipientType.TO, "to@no-spam.com"
- );
- mimeMessage.setSubject("Testing Subject");
- mimeMessage.setContent(
- "<b>Testing Content.</b>",
- "text/html; charset=utf-8"
- );
- mailSender.send(mimeMessage);
- }
- public static void sample2(JavaMailSender mailSender) throws Exception {
- MimeMessage mimeMessage = mailSender.createMimeMessage();
- MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8");
- message.setFrom("from@no-spam.com");
- message.setTo("to@no-spam.com");
- message.setSubject("Testing Subject");
- message.setText("<b>Testing Content.</b>", true);
- mailSender.send(mimeMessage);
- }
- public static void sample3(JavaMailSender mailSender) {
- SimpleMailMessage message = new SimpleMailMessage();
- message.setFrom("from@no-spam.com");
- message.setTo("to@no-spam.com");
- message.setSubject("Testing Subject");
- message.setText("Testing Content.");
- mailSender.send(message);
- }
- }
參考自:
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 回應:
張貼留言