美文网首页
使用javamail发信过程中的一些问题及解决方法

使用javamail发信过程中的一些问题及解决方法

作者: PengPengPro | 来源:发表于2017-01-17 21:13 被阅读27次
    private MimeMessage createMessage(String title, String content) {
            Properties properties = System.getProperties();
            properties.put(KEY_MAIL_HOST, VALUE_MAIL_HOST);
            properties.put(KEY_MAIL_AUTH, VALUE_MAIL_AUTH);
            SocketFactory ssf = javax.net.ssl.SSLSocketFactory.getDefault();
            properties.put("mail.smtp.ssl.enable", "true"); 
            properties.put("mail.smtp.ssl.socketFactory",ssf);
            properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    
    //      properties.put("mail.smtp.starttls.enable","true");
            properties.put("mail.smtp.timeout", "25000");
    
            // 开启debug调试
    //      properties.put("mail.debug", "true");
            // 发送服务器需要身份验证
            properties.put("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            properties.put("mail.host", "smtp.qq.com");
            // 发送邮件协议名称
            properties.put("mail.transport.protocol", "smtp");
            properties.put("mail.smtp.port", "465");
            properties.put("mail.smtp.socketFactory.port", "465");
    
            properties.put("mail.smtp.ssl.enable", "true");
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            properties.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            
            Session session = Session.getInstance(properties, getAuthenticator());
            // Session session = Session.getInstance(properties);
            MimeMessage mimeMessage = new MimeMessage(session);
            try {
                mimeMessage.setFrom(new InternetAddress(SENDER_NAME));
                InternetAddress[] addresses = new InternetAddress[] { new InternetAddress(SENDER_NAME)};
                mimeMessage.setRecipients(Message.RecipientType.TO, addresses);
                mimeMessage.setSubject(title);
                MimeBodyPart textPart = new MimeBodyPart();
                //textPart.setContent(content, "text/html");
    //          textPart.setContent(content, "text/html;charset=UTF-8");
                textPart.setContent(content, "text/html;charset=gbk");
    //          textPart.setHeader("Content-Transfer-Encoding", "base64");
    //          textPart.setHeader("Content-Transfer-Encoding", "quoted-printable");
                Multipart multipart = new MimeMultipart();
                multipart.addBodyPart(textPart);
    //          mimeMessage.setHeader( "Content-Transfer-Encoding", "base64" );
    //          mimeMessage.setHeader("Content-Transfer-Encoding", "quoted-printable");
                mimeMessage.setContent(multipart);
                mimeMessage.setSentDate(new Date());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return mimeMessage;
        }
    

    相关文章

      网友评论

          本文标题:使用javamail发信过程中的一些问题及解决方法

          本文链接:https://www.haomeiwen.com/subject/ivcvbttx.html