Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Unicode
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML-to-XML
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
SFTP
Signatures
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
Upload
XML
XMP
Zip

More Examples...
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
Bzip2
LZW

 

 

 

 

 

 

 

Load MHT and Send as Email

Load a .MHT file and send it as email.

Download Chilkat Java Library

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    //  The mailman object is used for sending and receiving email.
    CkMailMan mailman = new CkMailMan();

    //  Any string argument automatically begins the 30-day trial.
    boolean success;
    success = mailman.UnlockComponent("30-day trial");
    if (success != true) {
        System.out.println("Component unlock failed");
        return;
    }

    //  Set the SMTP server.
    mailman.put_SmtpHost("smtp.comcast.net");

    //  Create a new email object
    CkEmail email = new CkEmail();

    //  MHT is MIME and therefore can be loaded directly
    //  into an email object via the email.LoadEml method.
    //  However, if the HTML sub-part of the MIME contains
    //  header fields such as this:

    //  Content-ID: <BalanceSheet>
    //  Content-Disposition: inline;
    //  	filename="BalanceSheet"
    //  Content-Type: text/html;
    //  	name="BalanceSheet";
    //  	charset="utf-8"

    //  Microsoft Outlook will interpret this as an attached HTML file
    //  rather than the HTML body of the email.  Interestingly,
    //  Mozilla Thunderbird handles it correctly, but Outlook does not.
    //  Google's GMail also handles it correctly.  Yahoo Mail doesn't
    //  know what to do and displays nothing -- not even the "attachment".

    //  Rather than loading the MHT into the email object, we'll
    //  first load it into a Chilkat MIME object and check for
    //  these headers.  We want the header fields of the HTML sub-part
    //  to look like this:

    //  Content-Type: text/html;
    //  	charset="utf-8"

    //  To do so, we'll remove the Content-ID and Content-Disposition
    //  header fields, and the "name" attribute from the
    //  Content-Type header.

    //  Note: Chilkat MIME is a separate product.  This code
    //  would require licenses to both Chilkat Email and Chilkat Mime,
    //  or alternatively a Chilkat Bundle license (which includes
    //  all Chilkat components).

    CkMime mime = new CkMime();

    success = mime.UnlockComponent("Anything for 30-day trial");
    if (success != true) {
        System.out.println("Failed to unlock MIME component");
        return;
    }

    success = mime.LoadMimeFile("test.mht");
    if (success != true) {
        System.out.println(mime.lastErrorText());
        return;
    }

    //  Is this a multipart/related?  If so, find the HTML part,
    //  which should be the 1st sub-part.
    if (mime.IsMultipartRelated() == true) {
        //  Find the HTML part.
        CkMime mimePart;
        int n;
        int i;
        n = (int) mime.get_NumParts();

        for (i = 0; i <= n - 1; i++) {
            mimePart = mime.GetPart(i);
            if (mimePart.IsHtml() == true) {
                break;
            }

        }

        if (!(mimePart == null )) {
            //  Remove these header fields:
            mimePart.SetHeaderField("Content-Disposition","");
            mimePart.SetHeaderField("Content-ID","");
            //  Remove the "name" attribute from the Content-Type header:
            mimePart.put_Name("");

        }

    }

    email.SetFromMimeText(mime.getMime());

    email.put_Subject("This is a test");

    email.put_From("Chilkat Support <support@chilkatsoft.com>");
    email.AddTo("Chilkat Admin","admin@chilkatsoft.com");

    success = mailman.SendEmail(email);
    if (success != true) {
        System.out.println(mailman.lastErrorText());
    }
    else {
        System.out.println("Mail Sent!");
    }

  }
}

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.