HOWTO: Send a Message Using CDO with Visual J++ID: Q216723
|
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:
Use the following steps to build and run this Visual J++ example:
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 kbGrpMsg
Version : WINDOWS:1.21,6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 7, 1999