This article shares the specific code for spring+maven to implement email sending for your reference. The specific content is as follows
Today I wanted to get an email to send it, and then I went to Baidu and found that many friends who use email to send it will encounter various problems, including me. I encountered some problems. Let me send it out and follow the steps and run it directly.
PS: The following source code comes from Baidu and is not written by an individual. Without further ado, the code in the picture above
project:
Code block
package com.baidu.action;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;/** * -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Parameter name: @param subject Email subject* Parameter name: @param content Email subject content* Parameter name: @param to recipient Email address* Description: Send email*/ public void sendMail(String subject, String content, String to) { simpleMailMessage.setSubject(subject); //Set the email subject simpleMailMessage.setTo(to); //Set the recipient simpleMailMessage.setText(content); //Set the email subject content mailSender.send(simpleMailMessage); //Send email} //Spring dependency injection public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) { this.simpleMailMessage = simpleMailMessage; } //Spring dependency injection public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; }}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-3.1.xsd"> <bean id="mailSender"> <!-- Server--> <property name="host" value="smtp.sina.com" /> <!-- Port number --> <property name="port" value="25" /> <!-- User name --> <property name="username" value="[email protected]" /> <!-- Password--> <property name="password" value="password need to be set in the mailbox, or the mailbox permissions are enabled" /> <!-- SMTP server verification--> <property name="javaMailProperties"> <props> <!-- Verification of identity--> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- Verification of identity--> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <!-- The EMAIL accounts I have used are all from NetEase. The following list is the SMTP server name and port number of NetEase: NetEase Mailbox SMTP server SMTP port POP3 server POP3 [email protected] smtp.126.com 25 pop3.126.com 110 @163.com smtp.163.com 25 pop3.163.com 110 @yeah.net smtp.yeah.net 25 pop3.yeah.net 110 --> <bean id="simpleMailMessage"> <!-- Sender email --> <property name="from" value="[email protected]" /> </bean> <bean id="simpleMail"> <property name="mailSender" ref="mailSender" /> <property name="simpleMailMessage" ref="simpleMailMessage" /> </bean></beans>
EmailTest.java
package com.baidu.test;import junit.framework.TestCase;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.baidu.action.Email;/** * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ { public void testSendMail() { ApplicationContext context = new ClassPathXmlApplicationContext("spring-smtp-mail.xml"); Email mail = (Email)context.getBean("simpleMail"); mail.sendMail("Title", "Content", "[email protected]"); //mail.sendMail("Title", "Content", "Recipient Email"); }}pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.baidu.maven</groupId> <artifactId>mailtest02</artifactId> <packaging>war</packaging>> <version>0.0.1-SNAPSHOT</version> <name>mailtest02 Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.13.RELEASE</version> </dependency> </dependencies> <build> <finalName>mailtest02</finalName> </build></project>
Test: EmailTest.java Start JUnit
If it cannot be sent out, enter the email address to perform SMTP settings, as shown in the figure:
Send a simple email to be the above content, I hope it can help you.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.