zoukankan      html  css  js  c++  java
  • 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;
        }
    
    }

    测试结果

  • 相关阅读:
    远程监控基础知识和故障排除
    感慨颇多
    物联网操作系统Hello China移植mile stone之一:移植基础版本V1.76发布
    iOS5系统API和5个开源库的JSON解析速度测试
    (译)iPhone: 用公开API创建带小数点的数字键盘 (OS 3.0, OS 4.0)
    [工具]Mac平台开发几个网络抓包工具(sniffer)
    [工具]Mac下非常好用的快捷终端Dterm
    【IOS】在SDK中打开其他接入应用的解决方案
    [开源]在iOS上实现Android风格的控件Toast
    [技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档
  • 原文地址:https://www.cnblogs.com/xuchangqi1/p/11133292.html
Copyright © 2011-2022 走看看