ssm+javamail实现邮件发送功能

首先需要进入你的qq邮箱中开启POP3/SMTP服务。

1.点击左上方设置--》账户

 

2.开启POP3/STMP服务,开启服务会有一个授权码,需要保存下来,配置到代码中。

 

3.引入pom依赖。

<dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.7</version>
    </dependency>

  

4.配置applicationContext.xml文件

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.qq.com"/>
        <property name="port" value="465"/>
        <property name="username" value="你的qq邮箱"/>
        <property name="password" value="pop3授权码"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.ssl.enable">true</prop>
            </props>
        </property>
    </bean>

  

5.编写工具类

import org.springframework.transaction.annotation.Transactional;
import javax.mail.MessagingException;

public interface MailSenderSrvServices {
    /*普通格式发送
    * @recipient 收件人地址
    * @subject 主题
    * @content 正文
    * */
    @Transactional
    void sendEmail(String recipient,String subject,String content);

    /*带抄送
    * */
    void sendHtmlEmail(String recipient,String subject,String content) throws MessagingException, Exception;

}

  

import com.xcq.service.MailSenderSrvServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service("MailSenderSrvService")
public class MailSenderSrvServiceImpl implements MailSenderSrvServices {
    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     *   JavaMailSenderImpl支持MimeMessages和SimpleMailMessages。
     * MimeMessages为复杂邮件模板,支持文本、附件、html、图片等。
     * SimpleMailMessages实现了MimeMessageHelper,为普通邮件模板,支持文本
     */


    /**
     * 描述:Spring 依赖注入
     * @author wanghaoyu
     * @date
     * @version 1.0
     * @param mailSender
     * @since 1.8
     *
     */
    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 单发
     *
     * @param recipient 收件人
     * @param subject 主题
     * @param content 内容
     */
    @Override
    public void sendEmail(String recipient,String subject,String content) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            messageHelper.setFrom("xx@qq.com");//发件人
            messageHelper.setTo(recipient);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);//true代表支持html格式
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
                e.printStackTrace();
        }


    }


    @Override
    public void sendHtmlEmail(String recipient,String subject,String content) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
            messageHelper.setFrom("xx@qq.com");//发件人
            messageHelper.setTo(recipient);
            messageHelper.setSubject(subject);
            messageHelper.setText(content,true);
            mimeMessage.setRecipients(Message.RecipientType.CC,"xx@qq.com");//抄送人
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

  

6.编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Tset{
    @Autowired
    private MailSenderSrvServices mailsend;


    public static void mail(String[] args){
        String to = "xxx@qq.com";  //收件人地址
        String subject = "这是一封新的邮件";   //邮件标题
        String content = "今天天气不错";    //邮件内容

        mailsend.sendEmail(to,subject,content);    //发送邮件
    }


    public MailSendSrvServices getMailsend(){
        return mailsend;
    }

    public void setMailsend(MailSendSrvServices mailsend){
        this.mailsend = mailsend;
    }

}

 

 

测试结果

 

 

阿里云年末钜惠,所有云产品最低0.9折起:https://www.aliyun.com/minisite/goods?taskCode=pintuan20201212&recordId=311509&userCode=7zfjfcvn

 

posted @ 2019-07-04 16:37  徐昌琦  阅读(2617)  评论(0编辑  收藏  举报