Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 216728 - Last Review: August 30, 2004 - Revision: 3.3
How To Send Message Using Outlook Object Model with Visual J++
This article was previously published under Q216728
This article contains a code sample demonstrating how to use Outlook Object Model (OOM) to send mail with Visual J++.
Follow these 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, select Outlook 98 Type Library, then click OK.
- 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.
- 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");
}
}
For more information about using OOM with Visual J++, please see the following article in the Microsoft Knowledge Base:
216726Â
(http://kbalertz.com/Feedback.aspx?kbNumber=216726/EN-US/
)
How to Create Task and Appointment using Outlook Object Model with Visual J++
APPLIES TO
- Microsoft Visual J++ 6.0 Standard Edition
- Microsoft Outlook 98 Standard Edition
| kbhowto kbmsg kboutlookobj KB216728 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate