Java 向gmail发送Base64编码图像

Java 向gmail发送Base64编码图像,java,base64,gmail,Java,Base64,Gmail,我正试图将Base64编码的图像发送到gmail,但我无法看到原始图像。 我只能看到编码的文本格式。 我想在gmail中看到原始图像,但我不知道如何在gamil中显示原始图像。我想显示html格式。方法名称是电子邮件发件人 private void emailSender(CompanyInfo companyInfo) { final String username = "rk.ramesh0609@gmail.com"; final String password

我正试图将Base64编码的图像发送到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();
        }

}
private void emailSender(CompanyInfo CompanyInfo){
最终字符串username=“rk。ramesh0609@gmail.com";
最终字符串密码=“########””;
Properties prop=新属性();
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,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
试一试{
Map inlineImage=newhashmap();
String body=companyInfo.getLocation();
最终匹配器匹配器=imgRegExp.Matcher(主体);
int i=0;
while(matcher.find()){
字符串src=matcher.group();
if(body.indexOf(src)!=-1){
字符串srcToken=“src=\”;
intx=src.indexOf(srcToken);
int y=src.indexOf(“\”,x+srcToken.length());
字符串srcText=src.substring(x+srcToken.length(),y);
字符串cid=“image”+i;
字符串newSrc=src.replace(srcText,“cid:+cid”);
inlineImage.put(cid,srcText.split(“,”[1]);
body=body.replace(src,newSrc);
i++;
}
}
Message Message=新的mimessage(会话);
message.setFrom(新的InternetAddress(“rk”)。ramesh0609@gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(“rameshparthi1991@gmail.com")
);
message.setSubject(“公司信息”);
message.setText(“公司名称:”+companyInfo.getComapanyName()
+“\n\n图片名称:”+companyInfo.getPictureName()
+“\n\n电话号码:”+companyInfo.getPhone()
+“\n\n电子邮件:”+companyInfo.getEmail()
//+“\n\n位置:”+companyInfo.getLocation()
);
BodyPart bp=新的MimeBodyPart();
setContent(body,“text/html;charset=ISO-8859-1”);
MimeMultipart mmp=新的MimeMultipart();
mmp.addBodyPart(bp);
迭代器it=inlineImage.entrySet().Iterator();
while(it.hasNext()){
条目对=it.next();
PreencodedMimeBodyPart pmp=新PreencodedMimeBodyPart(“base64”);
pmp.setHeader(“内容ID”,即“”);
pmp.setDisposition(MimeBodyPart.INLINE);
setText(pairs.getValue());
mmp.addBodyPart(pmp);
}
message.setContent(mmp);
Transport.send(message,message.getAllRecipients());
系统输出打印项次(“完成”);
}捕获(消息异常e){
e、 printStackTrace();
}
}

请提供最低限度的完整代码样本,以复制您的问题更新的完整代码