Microsoft KB Archive/216728

= How To Send Message Using Outlook Object Model with Visual J++ =

Article ID: 216728

Article Last Modified on 8/30/2004

-

APPLIES TO


 * Microsoft Visual J++ 6.0 Standard Edition
 * Microsoft Outlook 98 Standard Edition

-



This article was previously published under Q216728



SUMMARY
This article contains a code sample demonstrating how to use Outlook Object Model (OOM) to send mail with Visual J++.



MORE INFORMATION
Follow these steps to build and run this Visual J++ example:


 * 1) Start Visual J++ 6.0. Create a new Console Application project.
 * 2) In the Project-Explorer window, open your Project tree and double-click the Class1.java file created for you.
 * 3) From the Project menu, click Add COM Wrapper, select Outlook 98 Type Library, then click OK.
 * 4) Ignore the created code and replace it with the following code. Make sure you make the necessary changes whenever you see the "TO DO" comment.
 * 5) Compile and run the code.

Sample Code
import com.ms.com.*; import msoutl85.*;  //In order to use Outlook Object Model

public class Class1 { public static void main(String[] args) {   try {  // Start from the application _Application olApp = (_Application) new Application; // Get MAPI infostore NameSpace mapiNS = olApp.GetNamespace("MAPI");

// Create a null Parameter for later use Variant nullParam = new Variant; nullParam.noParam; // Open the Outbox MAPIFolder outbox = (MAPIFolder)mapiNS.GetDefaultFolder(               OlDefaultFolders.olFolderOutbox); // Create a new message _MailItem newMsg = (_MailItem)outbox.getItems.Add(nullParam); // Set the subject newMsg.setSubject("This is message from Jave!"); // Set the body newMsg.setBody("How are you doing today!"); //Create an attachment, we need to do some preparation

// Get the source for the attachment Variant source = new Variant; // TODO: you may change the file path source.putString("C:\\Autoexec.bat"); // TODO: you may change the DisplayName Variant displayName = new Variant; displayName.putString("Autoexec.bat"); // Specify the type of the attachment Variant type = new Variant; type.putInt(OlAttachmentType.olByValue); // Decide where you want to put the attachment Variant position = new Variant; position.putFloat(1000); // It is time to really create the attachment Attachment attachment = (Attachment) newMsg.getAttachments.Add(source, type, position, displayName); // Create a Recipient

// TO DO: change to your recipient's name! Recipient recipient = (Recipient) newMsg.getRecipients.Add( "MyName"); // Set type to the recipient recipient.setType(OlMailRecipientType.olTo); // Resolve the recipient recipient.getResolved; // Send the new message newMsg.Send; // Clean up   olApp = null; mapiNS = null; outbox = null; newMsg = null; recipient = null; attachment = null; }   catch(java.lang.Exception e)     { System.out.println( "Failed!" ); e.printStackTrace; }          System.out.println("Program is terminated"); } }

