Microsoft KB Archive/216723

= How To Send a Message Using CDO (1.x) with Visual J++ =

Article ID: 216723

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 1.21
 * Microsoft Visual J++ 6.0 Standard Edition

-



This article was previously published under Q216723



SUMMARY
This article contains sample code that demonstrates how to use CDO (1.2, 1.21) with Microsoft Visual J++ to send e-mail messages.

The following list summarizes what the sample code does:


 * 1) Creates a CDO (1.2, 1.21) session and logs on.
 * 2) Opens the Outbox folder.
 * 3) Creates a new message in Outbox.
 * 4) Creates a recipient for the new message.
 * 5) Creates an attachment to the new message.
 * 6) Sends the message.

Please notice how Variant and getDispatch are used in the sample code.



MORE INFORMATION
Use the following 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, make a reference to "Microsoft CDO (1.2 or 1.21) Library," and click OK.
 * 4) Ignore the created code and replace it with the following code. Make the necessary changes whenever you see "TO DO" in the code.

Sample Code
import com.ms.com.*; // Import the CDO 1.2 or 1.21 library import cdo.*; public class Class1 {  public static void main(String args[]) {    try {   // Create a session _Session session = (_Session) new Session; // Create a Variant null parameter for later use Variant nullParam = new Variant; nullParam.noParam; // Create a profile and logon Variant profileName = new Variant; //TODO: change to your profile profileName.putString( "" ); session.Logon( profileName, nullParam, nullParam, nullParam,                            nullParam, nullParam, nullParam ); // Go to the Outbox folder Folder outbox = (Folder) session.getOutbox.getDispatch; // Get the Messages collection Messages messages = (Messages) outbox.getMessages.getDispatch; // 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.setSubject( subject ); // Set the text(body)of the new message. Variant text = new Variant; text.putString( "How are you doing today?" ); newMsg.setText( text ); // Get the Attachments collection. Attachments attachments = (Attachments)newMsg.getAttachments.getDispatch; // Create a new attachment Attachment attachment = (Attachment)attachments.Add(nullParam,            nullParam, nullParam, nullParam ).getDispatch; // Set properties for attachment Variant name = new Variant; name.putString( "Autoexec.bat" ); attachment.setName( name ); Variant source = new Variant; // TODO: you may change the file path source.putString( "C:\\Autoexec.bat" ); attachment.setSource( source ); Variant position = new Variant; position.putInt( 1 ); attachment.setPosition( position ); Variant attType = new Variant; attType.putFloat(1); //CdoFileData attachment.setType( attType );

// Get the Recipients collection. Recipients recipients = (Recipients)newMsg.getRecipients.getDispatch; // Create a new recipient. Recipient recipient = (Recipient) recipients.Add( nullParam,            nullParam, nullParam, nullParam ).getDispatch; // Set the name to the new recipient. Variant recipName = new Variant; //TODO: change to your recipient's name recipName.putString( "" ); recipient.setName( recipName ); // Set the type on the new recipient. Variant type = new Variant; type.putInt( 1 );  // "To" recipient.setType( type ); // Resolve the new recipient. Variant showDialog = new Variant; showDialog.putBoolean( true ); recipient.Resolve( showDialog ); // Send the new message. newMsg.Send( nullParam, nullParam, nullParam ); // Finally, logoff session.Logoff; // Clean up   session = null; outbox = null; messages = null; newMsg = null; recipients = null; recipient = null; attachment = null; }    catch( Exception e ) {        e.printStackTrace; }    System.out.println( "Mail is sent and project terminates!" ); } }

Keywords: kbhowto kbmsg kbjava kbfaq KB216723

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.