Java Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

Java Examples

Quick Start
Java Unicode
Java Certs
Java Email
Java Encryption
Java FTP
HTML-to-XML
Java HTTP
Java IMAP
Java MHT
Java MIME
Java RSA
Java S/MIME
Java Signatures
Java Socket
Java Spider
Java Tar
Java Upload
Java XML
Java XMP
Java Zip

More Examples...
Email Object
POP3
SMTP
RSS
Atom
String
Byte Array
Self-Extractor
Service
PPMD
Deflate
DH Key Exchange
DSA
SSH Key
SSH
SSH Tunnel
SFTP

Unreleased...
Bzip2
LZW
Bz2
Icon

 

 

 

 

 

 

 

Send Email with Attachments

Download Chilkat Java Library

Java example code to send email with attachments.

// Chilkat Java Example Program
	
import com.chilkatsoft.CkMailMan;
import com.chilkatsoft.CkEmail;
import com.chilkatsoft.CkString;

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

  // Sending email with attachments in Java.
  public static void main(String argv[]) 
  {
    CkMailMan mailman = new CkMailMan();
    mailman.UnlockComponent("anything for 30-day trial");
    
    // Set your SMTP server's hostname
    mailman.put_SmtpHost("smtp.comcast.net");
    
    // If your SMTP server requires a login, set username/password
    //mailman.put_SmtpUsername("myUsername");
    //mailman.put_SmtpPassword("myPassword");
    
    // Create a simple email
    CkEmail email = new CkEmail();
    email.put_Subject("Sending mail from Java");
    email.put_Body("This email was sent from a Java program");
    email.put_From("Chilkat Support <support@chilkatsoft.com>");
    // Add a few recipients
    email.AddTo("Matt","matt@chilkatsoft.com");
    email.AddTo("TagTooga","admin@tagtooga.com");
    
    // Attachments can be added from files or in-memory data.
    // Returns true if the file was added successfully.  The content-type
    // is returned in the 2nd argument.  This is for the benefit of programs
    // that may need it (for whatever reason), but most users do not.
    CkString contentType = new CkString();
    boolean b = email.AddFileAttachment("exampleData/hamlet.xml",contentType);
    
    // You may add a text file attachment directly from a string in memory.
    // AddStringAttachment2 allows for the charset encoding to be directly specified.
    // AddStringAttachment automatically uses the utf-8 encoding for text file attachments.
    email.AddStringAttachment("test1.txt","This string is the content of test1.txt");
    email.AddStringAttachment2("test2.txt","This string is the content of test2.txt","iso-8859-1");
    	
    boolean success = mailman.SendEmail(email);
    if (!success)
    {
    	mailman.SaveLastError("lastError.txt");	
    }
    
  }
}






 

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

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