html java邮件中的图像既可以内联显示,也可以作为Yahoo Mail中的附件显示

时间:2015-03-18 12:28:55

标签: java email velocity

having same problem that raise in below link

我使用的代码是

public class MailSender {
private static final MedikmLogger logger = MedikmLogger
        .getLogger(MailSender.class);
private String SMTP_HOST_NAME, SMTP_AUTH_USER, SMTP_AUTH_PWD;
 private static String MedikmDicomPath=AppConfig.getAppConfig().getProperty("images");
public void postMail(String recipients[], String subject, String message,
        String from) throws MessagingException {
    try{
        Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.port", "587");
    ApplicationSource applicationSource = ApplicationSource.getAppConfig();
    SMTP_HOST_NAME = applicationSource.getProperty("SMTP_SERVER");
    SMTP_AUTH_USER = applicationSource.getProperty("MAIL_SENDER");
    SMTP_AUTH_PWD = applicationSource.getProperty("MAIL_SENDER_PASSWORD");
    /*
     * logger.log(Level.SEVERE," ############### SMTP_AUTH_PWD ############### "
     * +SMTP_AUTH_PWD); logger.log(Level.SEVERE,
     * " ############### SMTP_HOST_NAME ############### "+SMTP_HOST_NAME);
     * logger
     * .log(Level.SEVERE," ############### SMTP_AUTH_USER ############### "
     * +SMTP_AUTH_USER);
     */
    props.put("mail.smtp.host", SMTP_HOST_NAME);
    props.put("mail.smtp.auth", "true");

    Authenticator auth = new SMTPAuthenticator();

    Session session = Session.getDefaultInstance(props, auth);
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    // Setting the Subject and Content Type
    msg.setSubject(subject);
    System.out.println("++++++++++++++++MedikmDicomPath++++++++++++++++++"+MedikmDicomPath);

    MimeMultipart multipart = new MimeMultipart("related");


    VelocityEngine ve = new VelocityEngine();
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
   ve.init();
   /*  next, get the Template  */
   Template t = ve.getTemplate("templates/emailtemplate.vm");
   System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=TTTT-=-=-=-=-=-=-"+t);
   /*  create a context and add data */
   VelocityContext context = new VelocityContext();
   context.put("message", message);
   /* now render the template into a StringWriter */
   StringWriter writer = new StringWriter();
   t.merge( context, writer );
   /* show the World */
   System.out.println("=-=-=-=-=-=-=-=-=-=-=WRITER-=-=-=-=-=-=-=-=-=-"+ writer.toString() );    


    String messageText = messageText(t, context);
    BodyPart part= new  MimeBodyPart();
    part.setContent(messageText, "text/html");
    // add it
    multipart.addBodyPart(part);
    BodyPart messageBodyPart = new MimeBodyPart();
    String f= this.getClass().getClassLoader().getResource("").getFile();
    String pathArr[] = f.split("/WEB-INF/classes/");
    FileDataSource fds = new FileDataSource(pathArr[0]+"/images/homelogo.png");
    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.addHeader("Content-ID","<senny>");
    // add it
    multipart.addBodyPart(messageBodyPart);




    MimeBodyPart messageBodyPart2 = new MimeBodyPart();
   // String f1= this.getClass().getClassLoader().getResource("").getFile();
   FileDataSource fds1 = new FileDataSource(pathArr[0]+"/images/rpci-logo.png");
    messageBodyPart2.setDataHandler(new DataHandler(fds1));
    messageBodyPart2.addHeader("Content-ID","<image>");
    // add it
    multipart.addBodyPart(messageBodyPart2);
    msg.setContent(multipart);
    //msg.setContent(messageText, "text/html");
    System.out.println("Sending Message");
    Transport.send(msg);
    System.out.println("Message Sent");
    }catch(Exception e){
        e.printStackTrace();
    }

}

private class SMTPAuthenticator extends javax.mail.Authenticator {

    public PasswordAuthentication getPasswordAuthentication() {
        String username = SMTP_AUTH_USER;
        String password = SMTP_AUTH_PWD;
        return new PasswordAuthentication(username, password);
    }
}
private String messageText(Template template, VelocityContext velocityContext){
    StringWriter stringWriter = new StringWriter();
    template.merge(velocityContext, stringWriter);
    return stringWriter.toString();
}

} 上面的代码在gmail中运行正常但是当相同的代码用于向雅虎发送邮件时它显示图像和附件中添加的图像也请帮助..

1 个答案:

答案 0 :(得分:0)

    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.addHeader("Content-ID", "<senny>");
    messageBodyPart.addHeader("Content-Type", "image/png");

通过添加以上行问题得到解决,谢谢你们每个人的时间......