Mht mail with attachments

Posted on 2004-10-22
Last Modified: 2013-11-24
Im working with JavaMail and I need to send MHT mails and MHT created in Word with attachments.
The point is to send HTML mails with attachments.

Any help?

Question by:mjuri
    LVL 9

    Expert Comment

    I'm unfamiliar with the package you're using. But refer to the documentation with it. Details should be specified there.

    HTH :)
    LVL 13

    Accepted Solution

    for emails to be sent make sure to send the content type to "text/html" a sample code will look like this:

      protected void sendMail( MailValueObject theMail  )
            props.put(ENVIRONMENT_PROPERTIES_MAIL_SMTP_USER, user);
            props.put(ENVIRONMENT_PROPERTIES_MAIL_SMTP_HOST, host);
            mailSession = Session.getInstance(props, null);

            Message msg = new MimeMessage( mailSession );
    //        msg.setFrom();
            msg.setFrom( new InternetAddress( theMail.getFromAddress() ) );
            msg.setRecipients( Message.RecipientType.TO,
                                InternetAddress.parse(theMail.getToAddress() , false ) );
            if( theMail.getCcAddress() != null)
                                InternetAddress.parse(theMail.getCcAddress(), false));
            if( theMail.getBccAddress() != null)
              msg.setRecipients( Message.RecipientType.BCC,
                                      InternetAddress.parse(theMail.getBccAddress() , false ) );
            msg.setSubject( theMail.getSubject() );

            msg.setContent( theMail.getBodyText(), "text/html");

            msg.setHeader("X-Mailer", mailer);
            msg.setSentDate( getCurrentTimeStamp() );


    where theMail is a normal been with the its member fields represents the single mail fields
    LVL 86

    Assisted Solution

    You just need to send the mht file as an attachment. Example here:

    Author Comment

    In this case when the email is open, the email body to show is HTML or simple text?
    LVL 92

    Assisted Solution

    here's an example of sending an image attachment with a html mail:

    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

    public class HtmlImageExample {
      public static void main (String args[]) throws Exception {
        String host = args[0];
        String from = args[1];
        String to = args[2];
        String file = args[3];

        // Get system properties
        Properties props = System.getProperties();

        // Setup mail server
        props.put("", host);

        // Get session
        Session session = Session.getDefaultInstance(props, null);

        // Create the message
        Message message = new MimeMessage(session);

        // Fill its headers
        message.setSubject("Embedded Image");
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Create your new message part
        BodyPart messageBodyPart = new MimeBodyPart();

        // Set the HTML content, be sure it references the attachment
        String htmlText = "<H1>Hello</H1>" +
          "<img src=\"cid:memememe\">";

        // Set the content of the body part
        messageBodyPart.setContent(htmlText, "text/html");

        // Create a related multi-part to combine the parts
        MimeMultipart multipart = new MimeMultipart("related");

        // Add body part to multipart

        // Create part for the image
        messageBodyPart = new MimeBodyPart();

        // Fetch the image and associate to part
        DataSource fds = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(fds));

        // Add a header to connect to the HTML

        // Add part to multi-part

        // Associate multi-part with message

        // Send message
    LVL 86

    Expert Comment

    In this case when the email is open, the email body to show is HTML or simple text?

    You can choose whatever you want. What you can't do is to have the whole mht file automatically integrated *as* the body of the mail. You'd have to unpack it to do that

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    933 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now