Maven project adds sending mail function

Maven project adds sending mail function

  1. Add jar package dependency to maven project
			<!-- -->
			<dependency>
		      <groupId>com.sun.mail</groupId>
		      <artifactId>javax.mail</artifactId>
		      <version>${javax.mail.version}</version>
			</dependency>
 

The version is:

  1. Get mail server configuration information:

    First set up smtp and pop3 services in the mailbox and get the authorization code:

mail.properties file:

mail.smtp.host=smtp.126.com
mail.smtp.username=gloryfung@126.com
mail.smtp.password=xxxxx
mail.smtp.defaultEncoding=utf-8
mail.smtp.auth=true
mail.smtp.timeout=20000
 
  1. Configure spring-mybatis.xml or applicationContext-dao.xml. Import the properties of the properties file in the configuration file:

	 <!-- -->
	<context:property-placeholder location="classpath:conf/mail.properties" ignore-unresolvable="true"/>
 

Note that when loading multiple configurations, the ignore-unresolvable="true" attribute must be added. When there are multiple property-placeholders in the configuration file, the placeholders that cannot be resolved will be ignored. For detailed explanation, please refer to this great god. 'S blog: blog.csdn.net/ilovec1/art...

  1. Inject a bean interface for email sending in spring-mybatis.xml or applicationContext-dao.xml:
	<!-- -->
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
	     <property name="host" value="${mail.smtp.host}"/>
	     <property name="username" value="${mail.smtp.username}"/>
	     <property name="password" value="${mail.smtp.password}"/>
	     <property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
	     <property name="javaMailProperties">
	         <props>
	             <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
	             <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
	         </props>
	     </property>
	</bean>
 

5. Write the test class:

package cn.idragonboat.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import cn.idragonboat.service.SendMailService;

@Service
public class SendMailServiceImpl implements SendMailService{

	@Autowired
    private JavaMailSender javaMailSender;//spring bean
	
	@Override
	public Object sendMail() {
		MimeMessage mMessage=javaMailSender.createMimeMessage();//
        MimeMessageHelper mMessageHelper;
        Properties prop = new Properties();
        String from;
        try {
           //
            prop.load(this.getClass().getResourceAsStream("/conf/mail.properties"));
            from = prop.get("mail.smtp.username")+"";
            mMessageHelper=new MimeMessageHelper(mMessage,true);
            mMessageHelper.setFrom(from);//
            mMessageHelper.setTo("844264382@qq.com");//
            mMessageHelper.setSubject("Spring ");//

            mMessageHelper.setText(" ");
            File file=new File("C:/Users/84426/Pictures/Saved Pictures/code.png");//
            FileSystemResource resource=new FileSystemResource(file);
            mMessageHelper.addInline("code", resource);//id, 
            mMessageHelper.addAttachment(" .png", resource);//
            javaMailSender.send(mMessage);//
        } catch (Exception e) {
			System.out.println("e:" + e);
		}
        return " ";
		
	}

}

 
  1. Reinstall the parent project, clean the aggregation project, install to the local warehouse, and start the project.

  2. Foreground scheduling: http://localhost:8080/mail/send