Spring mail service: Maven + Spring SMTP Mail

1  spring-smtp-mail.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <!-- 服务器 -->
        <property name="host" value="smtp.163.com" />
        <!-- 端口号 -->
        <property name="port" value="25" />
        <!-- 用户名 -->
        <property name="username "value =" ******* @ 163.com "/> 
        <-! the SMTP server authentication ->
        <-! Password ->
        <property name="password" value="*****" />
        <Property name = "javaMailProperties"> 
            <props> 
                <-! authenticate -> 
                <prop Key = "mail.smtp.auth"> to true </ prop> 
            </ props> 
        </ Property> 
    </ bean> 
    < ! - 
       currently I used the account eMAIL is NetEase, NetEase listed below SMTP server name and port number: 
        Netease mail SMTP server port SMTP POP3 server POP3 port 
        @ 126.com smtp.126.com 25 pop3.126. 110 COM 
        @ 163.com smtp.163.com pop3.163.com 25 110 
        @ 25 pop3.yeah.net yeah.net smtp.yeah.net 110 
    -> 

    <the bean ID = "SimpleMailMessage" = class "ORG.springframework.mail.SimpleMailMessage">
        <!-- 发件人email -->
        <property name="from" value="******@163.com" />
        <!--
         收件人email
        <property name="to" value="[email protected]" />
        email主题(标题)
        <property name="subject" value="Subject" />
        email主题内容
        <property name="text">
          <value>ContentText</value>
        </property>
        -->
    </bean>

    <bean id="simpleMail" class="com.richard.config.mailConfig">
        <property name="mailSender" ref="mailSender" />
        <property name="simpleMailMessage" ref="simpleMailMessage" />
    </bean>

</beans>

  2 mailConfig class

mailConfig class {public 
	Private MailSender mailSender; 
	Private SimpleMailMessage SimpleMailMessage; 
	
	/ ** 
	 * @ method name: sendMail 
	 * @ Parameter name: @param subject message subject 
	 * @ Parameter name: @param content mail subject content 
	 * @ Parameter name: @param to recipient email address 
	 * @ descriptors: send a message 
	 * / 
	public void sendMail (String Subject, Content String, String to) { 
		simpleMailMessage.setSubject (Subject); // set the message subject 
		simpleMailMessage.setTo (to); // set recipient to 
		simpleMailMessage.setText (content); // set the message subject matter 
		mailSender.send (simpleMailMessage); // send a message 
	} 
	// the Spring dependency injection 
	public void setSimpleMailMessage (SimpleMailMessage simpleMailMessage) { 
		this.simpleMailMessage = SimpleMailMessage; 
	} 
	// the Spring dependency injection
	public void setMailSender(MailSender mailSender) {
		this.mailSender = mailSender;
	}
}

  3 test category

@Test
public void sendEmail(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-smtp-mail.xml");
mailConfig mail = (mailConfig)context.getBean("simpleMail");
mail.sendMail("Spring SMTP Mail Subject", "Spring SMTP Mail Text", "*******@qq.com");
//mail.sendMail("标题", "内容", "收件人邮箱");
}

Guess you like

Origin www.cnblogs.com/yachao1120/p/11784358.html