zoukankan      html  css  js  c++  java
  • Spring通过Gmail SMTP服务器MailSender发送电子邮件

    Spring提供了一个有用的“org.springframework.mail.javamail.JavaMailSenderImpl”类,通过JavaMail API 简化邮件发送过程。这里有一个项目中使用Spring “JavaMailSenderImpl”通过Gmail SMTP服务器发送电子邮件。
    1. Spring邮件发件人
    Java 类使用 Spring 的 MailSender 接口发送电子邮件。

    File : MailMail.java

    package com.yiibai.common;
    
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    public class MailMail
    {
    	private MailSender mailSender;
    	
    	public void setMailSender(MailSender mailSender) {
    		this.mailSender = mailSender;
    	}
    	
    	public void sendMail(String from, String to, String subject, String msg) {
    
    		SimpleMailMessage message = new SimpleMailMessage();
    		
    		message.setFrom(from);
    		message.setTo(to);
    		message.setSubject(subject);
    		message.setText(msg);
    		mailSender.send(message);	
    	}
    }
    2. bean配置文件
    配置 mailSender bean 并指定Gmail的SMTP服务器电子邮件的详细信息。

    Gmail的配置细节(这里是墙,该翻的翻) – http://mail.google.com/support/bin/answer.py?hl=en&answer=13287

    File : Spring-Mail.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <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="yiibai.com@gmail.com" />
    	<property name="password" value="password" />
    		
    	<property name="javaMailProperties">
    	   <props>
           	      <prop key="mail.smtp.auth">true</prop>
           	      <prop key="mail.smtp.starttls.enable">true</prop>
           	   </props>
    	</property>
    </bean>
    	
    <bean id="mailMail" class="com.yiibai.common.MailMail">
    	<property name="mailSender" ref="mailSender" />
    </bean>
    	
    </beans>

    运行它

    package com.yiibai.common;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App 
    {
        public static void main( String[] args )
        {
        	ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Spring-Mail.xml");
        	 
        	MailMail mm = (MailMail) context.getBean("mailMail");
            mm.sendMail("from@no-spam.com",
        		   "to@no-spam.com",
        		   "Testing123", 
        		   "Testing only 
    
     Hello Spring Email Sender");
            
        }
    }
     
    下载源代码 –  http://pan.baidu.com/s/1gepbWEf
  • 相关阅读:
    DeepIn系统使用和相关软件安装
    在JDK11中生成JRE11的方法
    IIS 7 中设置文件上传大小的方法
    在服务器上发布MVC5的应用
    安装了多个Oracle11g的客户端,哪个客户端的tnsnames.ora会起作用?
    配置putty或SecureCRT防止SSH连接中断
    借助FRP反向代理实现内网穿透
    你不知道的hostname命令
    Perl脚本通过Expect登陆多台设备批量执行命令并Log
    Linux内核参数配置
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367578.html
Copyright © 2011-2022 走看看