![]() |
Languages »
C# »
How To
Intermediate
License: The Code Project Open License (CPOL)
Send Lotus Notes Email Using C#By kheaton1111How to send email to Lotus Notes from C#. |
C# (C# 1.0, C# 2.0, C# 3.0), VB (VB 8.0, VB 9.0), Windows (WinXP, Win2003), .NET (.NET 2.0), Visual Studio (VS2005, VS2008), COM
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

I had this problem at work; I needed to send a notification via Lotus Notes Email with a description of the error (ex.Message) and the name of the file that contained the error. Example: looking up values in a database that are not supplied in an XML file that I use to create a pipe delimited flat file going to another system. This would cause the flat file to not be processed; it would instead be moved to an error folder, and an event log entry would be created. Of course, this doesn't help the users, but an email would. The Internet doesn't have much in the way of C#-To-Lotus Notes, so I took some VBA samples, a few C# GUI samples, and modified them for my own needs, since what was there doesn't work for use in a Windows service. This gives the service the use of an application, and best of all, I don't have to do anything but check my email.
I also use this procedure to create an email to notify another user that a file was successfully processed and is awaiting them to import into the ERP system.
What this does:
LeadTechNotFoundException). I use this to tell the user to go into the database, add a lead tech, and then move the XML file from the error folder back into the processing folder. The service picks it up via a FileSystemWatchers and re-processes it. If they copy instead of move, I check for the file and delete it if it exists. Simple, eh?Set your reference to the Domino COM Object.

This code can be used as is or modified. It's fully functional (at least on my systems).
static void SendNotesErrorMail( string err, string file)
{
//Pass in file name
string filename = file;
//Pass in error message from TryCatch
string errMessage = err;
//Create new notes session
NotesSession _notesSession = new NotesSession();
//Initialize Notes Database to null; nothing in VB.
NotesDatabase _notesDataBase = null;
//Initialize Notes Document to null; nothing in VB.
NotesDocument _notesDocument = null;
//Notes Server Name in form of: ServerName/Domain.
string sServerName = ConfigurationManager.AppSettings [ "ServerName" ];
//Mail File is in form of: mail\\userName.nsf
string sMailFile = ConfigurationManager.AppSettings [ "MailFile" ];
string password = ConfigurationManager.AppSettings [ "Password" ];
string sSendTo = ConfigurationManager.AppSettings [ "SendTo" ];
string sSubject = "Billing Error";
//required for send, since it's byRef and not byVal, gets set later.
object oItemValue = null;
//use string array to CC Send
string[] sCopyTo = new string[4];
sCopyTo [ 0 ] =
ConfigurationManager.AppSettings [ "Recipient0" ];
sCopyTo [ 1 ] =
ConfigurationManager.AppSettings [ "Recipient1" ];
sCopyTo [ 2 ] =
ConfigurationManager.AppSettings [ "Recipient2" ];
sCopyTo [ 3 ] =
ConfigurationManager.AppSettings [ "Recipient3" ];
//Initialize Notes Session
_notesSession.Initialize(password);
//Get Database via server name & c:\notes\data\mailfilename.nsf
//if not found set to false to not create one
_notesDataBase = _notesSession.GetDatabase(sServerName, sMailFile,
false);
//If the database is not already open then open it.
if ( !_notesDataBase.IsOpen )
{
_notesDataBase.Open( );
}
//Create the notes document
_notesDocument = _notesDataBase.CreateDocument();
//Set document type
_notesDocument.ReplaceItemValue(
"Form", "Memo");
//sent notes memo fields (To: CC: Bcc: Subject etc)
_notesDocument.ReplaceItemValue(
"SendTo", sSendTo);
_notesDocument.ReplaceItemValue(
"CopyTo", sCopyTo);
_notesDocument.ReplaceItemValue(
"Subject", sSubject);
//Set the body of the email. This allows you to use the appendtext
NotesRichTextItem _richTextItem = _notesDocument.CreateRichTextItem("Body");
//add lines to memo email body. the \r\n is needed for each new line.
_richTextItem.AppendText(
"Error: " + errMessage + "\r\n");
_richTextItem.AppendText(
"File: " + filename + "\r\n");
_richTextItem.AppendText(
"Resolution: " + resolution + "\r\n");
//send email & pass in byRef field, this case SendTo (always have this,
//cc or bcc may not always be there.
oItemValue = _notesDocument.GetItemValue(
"SendTo" );
_notesDocument.Send(
false, ref oItemValue);
//release resources.
_richTextItem =
null;
_notesDocument =
null;
_notesDataBase =
null;
_notesSession =
null;
}
This was a pain. There are a lot of VBA / Access / Excel with Lotus Notes how-to's etc. There is also a pretty nice example here from another user, with a GUI.
The changes I will make is to check a value in the app.config file and set the array size from that count. I also need to add some additional sections to the "resolution" section for my users. I may post a sample project, if I can get some time.
This is my first post here, so I hope it helps someone out.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Sep 2008 Editor: Smitha Vijayan |
Copyright 2008 by kheaton1111 Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |