Creating and Sending a Message (Java)

Creating and Sending a Message (Java)

This example is a standalone application, not an applet. It is a complete program demonstrating the basic procedures for initiating a CDO Library message. Within the framework of logging on to and off from a session, it creates a Message object, sets its requisite properties, and sends it to its recipients through the MAPI system.

import cdo.*;
import com.ms.com.*;
import java.io.*;

public class Sample1
{
  public static void main(String args[])
  {
    // Call the class factory.
    _Session session = (_Session) new Session();

    Variant nullParam = new Variant();
    nullParam.noParam();
    Messages messages = null;

    try    // First, log on.
    {
      Variant profileName = new Variant();
      profileName.putString( "MyProfile" );
      session.Logon( profileName, nullParam, nullParam, nullParam,
                                  nullParam, nullParam, nullParam );
    }
    catch( Exception e )
    {
      System.out.println( "Logon failed!" );
      e.printStackTrace();
    }

    try    // Get the messages collection in the outbox.
    {
      Folder outBox = (Folder) session.getOutbox().getDispatch();
      messages = (Messages) outBox.getMessages().getDispatch();
    }
    catch( Exception e )
    {
      System.out.println( "Failed to get the outbox messages collection!" );
      e.printStackTrace();
    }

    try    // Now create, populate, and send the message.
    {
      // Create a new message in the outbox.
      Message newMsg = (Message) messages.Add( nullParam, nullParam,
                                nullParam, nullParam ).getDispatch();

      // Set the subject of the new message.
      Variant subject = new Variant();
      subject.putString( "This is a message from Java!" );
      newMsg.putSubject( subject );

      // Set the text (body) of the new message.
      Variant text = new Variant();
      text.putString( "This is the message text." );
      newMsg.putText( text );

      // Get the Recipients collection.
      Recipients recips = (Recipients) newMsg.getRecipients().getDispatch();

      // Create a new recipient in the Recipients collection.
      Recipient recip = (Recipient) recips.Add( nullParam, nullParam,
                                nullParam, nullParam ).getDispatch();

      // Set the name on the new recipient.
      Variant name = new Variant();
      name.putString( "somebody" );
      recip.putName( name );

      // Set the type on the new recipient.
      Variant type = new Variant();
      type.putInt( 1 ); // "To" recipient
      recip.putType( type );

      // Resolve the new recipient.
      Variant showDialog = new Variant();
      showDialog.putBoolean( true );
      recip.Resolve( showDialog );

      // Make the new message permanent.
      Variant refresh = new Variant();
      Variant makePermanent = new Variant();
      refresh.putBoolean( true );
      makePermanent.putBoolean( true );
      newMsg.Update( makePermanent, refresh );

      // Send the new message.
      Variant saveCopy = new Variant();
      saveCopy.noParam();
      showDialog.noParam(); // already created under Resolve()
      Variant wndHandle = new Variant();
      wndHandle.noParam();
      newMsg.Send( saveCopy, showDialog, wndHandle );
    }
    catch( Exception e )
    {
      System.out.println( "Failed to create/send a new message!" );
    }

    try    // Finally, log off.
    {
      session.Logoff();
    }
    catch( Exception e )
    {
      System.out.println( "Logoff failed!" );
    }
    System.out.println( "All done!" );
  }
}
 

See Also

Concepts

Java Programming Examples