发送base 64编码的图像到gmail

时间:2019-11-21 04:19:00

标签: java base64 gmail

我正在尝试将基数为64的编码图像发送到gmail,但我看不到原始图像。    我只能看到编码的文本格式。    我想在gmail中看到原始图像,但我不知道如何在gamil中显示原始图像。我想显示html格式。方法名称是电子邮件发件人

private void emailSender(CompanyInfo companyInfo) {
     final String username = "rk.ramesh0609@gmail.com";
        final String password = "#######";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {


            Map<String, String> inlineImage = new HashMap<String, String>();
             String body = companyInfo.getLocation();
             final Matcher matcher = imgRegExp.matcher( body );
               int i = 0;
               while ( matcher.find() ) {
                      String src = matcher.group();
                      if ( body.indexOf( src ) != -1 ) {
                         String srcToken = "src=\"";
                         int x = src.indexOf( srcToken );
                         int y = src.indexOf( "\"", x + srcToken.length() );
                         String srcText = src.substring( x + srcToken.length(), y );
                         String cid = "image" + i;
                         String newSrc = src.replace( srcText, "cid:" + cid );
                         inlineImage.put( cid, srcText.split( "," )[1] );
                         body = body.replace( src, newSrc );
                         i++;
                      }
                   }




            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("rk.ramesh0609@gmail.com"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse("rameshparthi1991@gmail.com")
            );
            message.setSubject("Company info");
            message.setText("Company name:"+companyInfo.getComapanyName()
                    + "\n\n Pic Name:"+companyInfo.getPictureName()
                    + "\n\n Phone Number:"+companyInfo.getPhone()
                    + "\n\n Email:"+companyInfo.getEmail()
                    //+ "\n\n Location:"+companyInfo.getLocation()
                  );


            BodyPart bp = new MimeBodyPart();
            bp.setContent( body, "text/html; charset=ISO-8859-1" );
            MimeMultipart mmp = new MimeMultipart();
            mmp.addBodyPart( bp );
            Iterator<Entry<String, String>> it = inlineImage.entrySet().iterator();
            while ( it.hasNext() ) {
               Entry<String, String> pairs = it.next();
               PreencodedMimeBodyPart pmp = new PreencodedMimeBodyPart( "base64" );
               pmp.setHeader( "Content-ID", "<" + pairs.getKey() + ">" );
               pmp.setDisposition( MimeBodyPart.INLINE );
               pmp.setText( pairs.getValue() );
               mmp.addBodyPart( pmp );
            }
            message.setContent( mmp );

            Transport.send(message,message.getAllRecipients());

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }

}

0 个答案:

没有答案