HOWTO: Send Message Using Outlook Object Model with Visual J++

ID: Q216728


The information in this article applies to:


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");
  }
} 


REFERENCES

For more information about using OOM with Visual J++, please see the following article in the Microsoft Knowledge Base:

Q216726 How to Create Task and Appointment using Outlook Object Model with Visual J++

Additional query words: kbMsg kbOutlookObj kbOutlook850 kbOutlook98 kbVJ600 kbGrpMsg


Keywords          : kbMsg kbOutlook98 kbOutlookObj kbVJ600 kbOutlook850 
Version           : WINDOWS:6.0,98
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 27, 1999