[How To] Send email using Lotus Server asp.net c#
Today I'm going to explain how to send e-mails by using Lotus server configured machine. For this we need to include a DLL (which will be provided from Lotus application support team) called Domino. In the below image, you could see how I had refereed that in my asp.net web application
Also, add following lines in your app.config or web.config file. Following are the configurable part of Lotus server details such as server IP address, server port, lotus from address that configured in the server machine, Lotus from name displayed on emails that sending through our program, Lotus server machine mail address password to authenticate and Lotus key file to send mails.
This can be changed in the config file when it is moved to another environment like testing or production etc without changing our program. No need for separate build.
I have customized many things in the above code, attachments are passed as path to the attachment, not byte array. You need to pass exact path of the attachment like "D:/Demo/Test/Test.pdf".
I think, now you got the idea. If you have any doubts, just comment below. Remember that, this is configured like mail sending is done by using the same server that the Lotus mail is configured. Otherwise above code will not work.
Configurations
You need to use following namespace in your class file to use the classes and methods from the DLL in your code
using Domino;
Also, add following lines in your app.config or web.config file. Following are the configurable part of Lotus server details such as server IP address, server port, lotus from address that configured in the server machine, Lotus from name displayed on emails that sending through our program, Lotus server machine mail address password to authenticate and Lotus key file to send mails.
<add key="LotusServer" value=""/> <add key="LotusServerPort" value="0"/> <add key="LotusFromName" value="Test Mail"/> <add key="LotusFromAddress" value="mail/test"/> <add key="LotusFromPassword" value="test"/> <add key="LotusNotesNsf" value="test.nsf"/>
This can be changed in the config file when it is moved to another environment like testing or production etc without changing our program. No need for separate build.
Sending normal text mail using Lotus
Just refer the following code,
/// <summary> /// Send mail using lotus mail server. /// </summary> /// <param name="mailToArr">Mailing to addresses as string array.</param> /// <param name="strSubject">Mail subject as string.</param> /// <param name="strMessageBody">Mail body as string.</param> /// <param name="arrFiles">Paths of files need to be attached as string array.</param> /// <param name="CCarry">Mail CC addresses as string array.</param> /// <param name="BCCarry">Mail BCC addresses as string array.</param> /// <exception cref="System.Exception">Thrown when could not able to connect to lotus database.</exception> public static void SendLotusMail(string[] mailToArr, string strSubject, string strMessageBody, string[] arrFiles, string[] CCarry = null, string[] BCCarry = null) { try { object arr; arr = mailToArr; NotesSession ns = new NotesSession(); NotesDatabase db = null; NotesDocument doc = null; NotesDbDirectory dbDir = null; string strLotusServer = ConfigurationManager.AppSettings["LotusServer"].ToString(); string strWConFrom = ConfigurationManager.AppSettings["LotusFromAddress"].ToString(); string strPassword = ConfigurationManager.AppSettings["LotusFromPassword"].ToString(); if (ns != null) { ns.Initialize(strPassword); try { db = ns.CurrentDatabase; } catch (Exception) { } if (db == null) { dbDir = ns.GetDbDirectory(""); db = dbDir.OpenMailDatabase(); if (!db.IsOpen) { db.Open(); } } else if (!db.IsOpen) { db.Open(); } if (db != null) { ns.ConvertMime = false; NotesStream stream = ns.CreateStream(); doc = db.CreateDocument(); doc.ReplaceItemValue("Form", ConfigurationManager.AppSettings["LotusFromName"].ToString()); doc.ReplaceItemValue("From", ns.CommonUserName); doc.ReplaceItemValue("SendTo", mailToArr); if (CCarry != null && CCarry.Any()) { doc.ReplaceItemValue("CopyTo", CCarry); } if (BCCarry != null && BCCarry.Any()) { doc.ReplaceItemValue("BlindCopyTo", BCCarry); } doc.ReplaceItemValue("Subject", strSubject); NotesRichTextItem body = doc.CreateRichTextItem("Body"); strMessageBody = strMessageBody.Replace("<br/>", Environment.NewLine); body.AppendText(strMessageBody); if (arrFiles != null && arrFiles.Any()) { int Count = 0; foreach (string strFile in arrFiles) { NotesRichTextItem notesRtf = doc.CreateRichTextItem("Attachment" + Count.ToString()); NotesEmbeddedObject EmbedObj = notesRtf.EmbedObject(Domino.EMBED_TYPE.EMBED_ATTACHMENT, "", strFile, "Attachment" + Count.ToString()); Count++; } } doc.SaveMessageOnSend = false; doc.Send(false, ref arr); doc = null; body = null; stream = null; } else { throw new Exception("Could not connect to Lotus notes database"); } db = null; ns = null; dbDir = null; } } catch (Exception ex) { throw new Exception("Lotus Exception:" + ex.Message); } }
I have customized many things in the above code, attachments are passed as path to the attachment, not byte array. You need to pass exact path of the attachment like "D:/Demo/Test/Test.pdf".
Sending HTML content email using Lotus server
It is just like the above method and some changes needed. Modified full code is given below
/// <summary> /// Send mail using lotus mail server with HTML as Content. /// </summary> /// <param name="mailToArr">Mailing to addresses as string array.</param> /// <param name="strSubject">Mail subject as string.</param> /// <param name="strMessageBody">Mail body as string.</param> /// <param name="arrFiles">Paths of files need to be attached as string array.</param> /// <param name="CCarry">Mail CC addresses as string array.</param> /// <param name="BCCarry">Mail BCC addresses as string array.</param> /// <exception cref="System.Exception">Thrown when could not able to connect to lotus database.</exception> public static void SendLotusHTMLMail(string[] mailToArr, string strSubject, string strMessageBody, string[] arrFiles, string[] CCarry = null, string[] BCCarry = null) { object arr; arr = mailToArr; NotesSession ns = new NotesSession(); NotesDatabase db = null; NotesDocument doc = null; NotesDbDirectory dbDir = null; string strLotusServer = ConfigurationManager.AppSettings["LotusServer"].ToString(); string strWConFrom = ConfigurationManager.AppSettings["LotusFromAddress"].ToString(); string strPassword = ConfigurationManager.AppSettings["LotusFromPassword"].ToString(); try { if (ns != null) { ns.Initialize(strPassword); try { db = ns.CurrentDatabase; } catch (Exception) { } if (db == null) { dbDir = ns.GetDbDirectory(""); db = dbDir.OpenMailDatabase(); if (!db.IsOpen) { db.Open(); } } else if (!db.IsOpen) { db.Open(); } if (db != null) { ns.ConvertMime = false; NotesStream stream = ns.CreateStream(); doc = db.CreateDocument(); doc.ReplaceItemValue("Form", ConfigurationManager.AppSettings["LotusFromName"].ToString()); doc.ReplaceItemValue("From", ns.CommonUserName); doc.ReplaceItemValue("SendTo", mailToArr); if (CCarry != null && CCarry.Length > 0) { doc.ReplaceItemValue("CopyTo", CCarry); } if (BCCarry != null && BCCarry.Any()) { doc.ReplaceItemValue("BlindCopyTo", BCCarry); } doc.ReplaceItemValue("Subject", strSubject); NotesMIMEEntity body = doc.CreateMIMEEntity(); stream.WriteText(strMessageBody); if (arrFiles != null && arrFiles.Any()) { int Count = 0; foreach (string strFile in arrFiles) { NotesRichTextItem notesRtf = doc.CreateRichTextItem("Attachment" + Count.ToString()); NotesEmbeddedObject EmbedObj = notesRtf.EmbedObject(Domino.EMBED_TYPE.EMBED_ATTACHMENT, "", strFile, "Attachment" + Count.ToString()); Count++; } } body.SetContentFromText(stream, "text/html;charset=iso-8859-1", MIME_ENCODING.ENC_IDENTITY_8BIT); doc.SaveMessageOnSend = true; doc.Send(false, ref arr); doc = null; body = null; stream = null; ns.ConvertMime = true; } db = null; ns = null; } } catch (Exception ex) { throw new Exception("Lotus Exception:" + ex.Message); } }
I think, now you got the idea. If you have any doubts, just comment below. Remember that, this is configured like mail sending is done by using the same server that the Lotus mail is configured. Otherwise above code will not work.
[How To] Send email using Lotus Server asp.net c#
Reviewed by TechDoubts
on
11:41 PM
Rating:
Hi,
ReplyDeleteHow to call this method...Can you Help?
Sorry for the late reply. You don't need to create an object of the class. Just call like YourClassName.SendLotusMail();
Delete