java使用QQ邮箱

java 实现向QQ邮箱发送一般内容

            /**  
            * 向QQ邮箱发送消息  
            * @param sendToQQ  
            * @param content  
            * @throws GeneralSecurityException  
            * @throws MessagingException  
            */  
        public static void sendMessageToMail(String sendToQQ,String content) throws GeneralSecurityException, MessagingException {


                 Properties pro = new Properties();  
             //        开启debug调试  
                 pro.setProperty("mail.debug", "true");  
             //        发送服务器需要身份验证  
                 pro.setProperty("mail.smtp.auth", "true");  
             //        设置邮件服务器主机名  
                 pro.setProperty("mail.host", "smtp.qq.com");  
             //        发送邮件协议名称  
                 pro.setProperty("mail.transport.protocol",   "smtp");  



            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            pro.put("mail.smtp.ssl.enble", "true");
            pro.put("mail.smtp.ssl.socketFactory", sf);

            Session session = Session.getInstance(pro);

            Message msg = new MimeMessage(session);
            msg.setSubject("验证码");
            msg.setText(content);
            //  设置发送人
            msg.setFrom(new InternetAddress("[email protected]"));

            Transport transport = session.getTransport();
            // 设置密令
            transport.connect("smtp.qq.com", "[email protected]", "yrxypofwypbybgfh");
            //设置收件人
            transport.sendMessage(msg, new Address[]{new          InternetAddress(sendToQQ)});
            transport.close();
    }

向邮箱html格式文件

配置bean

 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
     <property name="host" value="smtp.qq.com" />
     <property name="username" value="[email protected]" />
     <property name="password" value="yrxypofwypbybgfh" />
     <property name="port" value="25" />
     <property name="javaMailProperties">
         <props>
             <prop key="mail.transport.protocol">smtpprop>
             <prop key="mail.smtp.auth">trueprop>
             <prop key="mail.smtp.starttls.enable">trueprop>

             <prop key="mail.debug">trueprop>
         props>
     property>
 bean>

调用程序

  JavaMailSender mailSender;
      /**  
        *   
        * @param url  可以是一个controller层的一个方法 
        * @param receive  接收人
        * @throws MessagingException  
        */  
    
    public  void sendCodeImg(String url,String receive) throws MessagingException {

        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");
        
        helper.setFrom("[email protected]");
        helper.setTo(receive);
        helper.setSubject("MiaoMiao影厅");
        helper.setText("",true);
        mailSender.send(msg) 
         }

你可能感兴趣的:(utils)