Maven工程添加 發送郵件 功能

  1. 在maven工程添加jar包依賴
<!--郵件發送-->
			<dependency>
		      <groupId>com.sun.mail</groupId>
		      <artifactId>javax.mail</artifactId>
		      <version>${javax.mail.version}</version>
			</dependency>
複製代碼

版本爲:java

  1. 獲取郵件服務器配置信息:spring

    先在郵箱中設置開啓smtp和pop3服務而後拿到受權碼:bash

mail.properties文件:服務器

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. 配置spring-mybatis.xml或者applicationContext-dao.xml。在配置文件中導入properties文件的屬性:

<!--郵件配置-->
	<context:property-placeholder location="classpath:conf/mail.properties" ignore-unresolvable="true"/>
複製代碼

注意加載多個配置的時候,均要加上ignore-unresolvable="true"屬性,是配置文件中存在多個property-placeholder時出現解析不了的佔位符進行忽略掉,詳細解釋能夠參考這位大神的博客:blog.csdn.net/ilovec1/art…mybatis

  1. 在spring-mybatis.xml或者applicationContext-dao.xml中注入一個郵件發送的bean接口:
<!--配置郵件接口-->
	<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.編寫測試類:app

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. 從新install parent工程,clean 聚合工程,再install到本地倉庫,啓動工程。maven

  2. 前臺調度: http://localhost:8080/mail/sendide

相關文章
相關標籤/搜索