HOWTO: Send a Message Using CDO with Visual J++
ID: Q216723
|
The information in this article applies to:
-
Collaboration Data Objects (CDO), version 1.21
-
Microsoft Visual J++, version 6.0
SUMMARY
This article contains sample code that demonstrates how to use CDO with Microsoft Visual J++ to send e-mail messages.
The following list summarizes what the sample code does:
- Creates a CDO session and logon.
- Opens the Outbox folder.
- Creates a new message in Outbox.
- Creates a recipient for the new message.
- Creates an attachment to the new message.
- 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:
- Start Visual J++ 6.0. Create a new Console Application project.
- In the Project Explorer window, open your Project tree and double-click the Class1.java file created for you.
- From the Project menu, click Add COM Wrapper, make a reference to "Microsoft CDO 1.21 Library," and click OK.
- Ignore the created code and replace it with the following code.
Make the necessary changes whenever you see TODO in the code.
Sample Code
import com.ms.com.*;
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( "<ProfileName>" );
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( "<MyName>" );
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!" );
}
}
Additional query words:
kbMsg kbcdo kbCDO121 kbJava
Keywords : kbCDO121 kbJava kbMsg kbVJ600 kbfaq kbGrpMsg
Version : WINDOWS:1.21,6.0
Platform : WINDOWS
Issue type : kbhowto