This article is exclusively sponsored by the Lebyte Java Architecture course

Environment to prepare

Create a Maven project and add dependencies to the POM.xml file

<! -- Spring core JAR dependencies -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.4. RELEASE</version>
</dependency>
<! -- Spring context support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.4. RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.4. RELEASE</version>
    <scope>test</scope>
</dependency>
<! -- Java Mail coordinate dependency -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.2</version>
</dependency>
Copy the code

Configure the mail sending bean

Set the bean label corresponding to mail sending in the spring.xml configuration file

<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <! -- Enable automatic scanning -->
    <context:component-scan base-package="com.xxxx"/>

    <! -- Mail sender (provides the Mail sending interface, MimeMessage for transparent creation of Java Mail, and Mail sending configuration) -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="port" value="25" />
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="username" value="Username"></property>
        <property name="password" value="Password"></property>
    </bean>

    <! -- Plain text mail object -->
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="Sender's email address" />
        <property name="subject" value="spring_mail" />
    </bean>

</beans>

Copy the code

Define interfaces and implementation classes

Defines the interface

package com.xxxx.springMail;

/** * defines the interface */
public interface OrderManager {
    void placeOrder(a);
}
Copy the code

Defining the implementation class

package com.xxxx.springMail;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class SimpleOrderManager implements OrderManager {

    @Resource
    private JavaMailSenderImpl mailSender;
    @Resource
    private SimpleMailMessage templateMessage;

    @Override
    public void placeOrder(a) {
        // Get the mail object
        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        // Set the email recipient's email address
        msg.setTo("Recipient's Email address");
        // Set the email content
        msg.setText("Hello Spring Mail");
        try{
            // Send an email
            this.mailSender.send(msg);
        } catch(MailException ex) { System.err.println(ex.getMessage()); }}}Copy the code

Mail sending test

/** ** Send email *@param args
  */
public static void main(String[] args) {
    ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
    SimpleOrderManager simpleOrderManager = 
        (SimpleOrderManager) ac.getBean("simpleOrderManager");
    simpleOrderManager.placeOrder();
}
Copy the code

Send attachments

/** * Send a message containing attachments *@param args
  * @throws MessagingException
  */
public static void main(String[] args) throws MessagingException {
    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    JavaMailSender mailSender= (JavaMailSender) ac.getBean("mailSender");
    MimeMessage message= mailSender.createMimeMessage();
    message.setSubject("spring_mail"); // The subject of the message
    // Create a gang class with attachments
    MimeMessageHelper helper = new MimeMessageHelper(message,true."utf-8");
    helper.setTo(new InternetAddress("Recipient's Email address"));// Set the receiver
    helper.setText("Thank you for ordering!"); // The content of the email
    helper.setFrom("Sender's email address"); / / the sender
    // Set attachments
    File file = new File("C:\ work\ email attachment.txt");
    // Add attachments
    helper.addAttachment(file.getName(), file);
    // Send an email
    mailSender.send(message);
}
Copy the code