SoFunction
Updated on 2025-03-07

Methods of sending emails using Lotus notes public mailbox in C#

Preface

The company's email system uses anti-human Lotus notes, dare you believe it?

Recently, we have to implement a function, the email reminder function, which is to automatically send reminder emails.

This problem took two days to deal with. Due to the company's many conditions and restrictions, it is impossible to directly call the interface for the company to send emails. It can only be achieved through other methods such as Lotus script, VBA, etc.

I used VBA code to send emails, but I actually implemented it n years ago

The code is as follows, and there are a lot of searches online

Function SendEmailbyNotesWithAttachement_2(Addresses, Attach, cc)
 strSubject = ("EMAIL").Range("B1")
 strbody = ("EMAIL").Range("A1")
 'Declare Variables
  Dim s As Object
  Dim db As Object
  Dim body As Object
  Dim bodyChild As Object
  Dim header As Object
  Dim stream As Object
  Dim host As String
  Dim message As Object
  ' Notes variables
  Set s = CreateObject("")
  Set db = 
  Set stream = 
  ' Turn off auto conversion to rtf
   = False
  ' Create message
  Set message = 
   = "memo"
   = strSubject
   = Split(Addresses, ";")
   = cc
   = True
  ' Create the body to hold HTML and attachment
  Set body = 
 'Child mime entity which is going to contain the HTML which we put in the stream
  Set bodyChild = ()
  Call (strbody)
  Call (stream, "text/HTML;charset=UTF-8", ENC_NONE)
  Call 
  Call 
  ' This will run though an array of attachment paths and add them to the email
  For i = 0 To UBound(Attach)
  strAttach = Attach(i)
  If Len(strAttach) > 0 And Len(Dir(strAttach)) > 0 Then
   ' Get the attachment file name
   pos = InStrRev(strAttach, "\")
   Filename = Right(strAttach, Len(strAttach) - pos)
   'A new child mime entity to hold a file attachment
   Set bodyChild = ()
   Set header = ("Content-Type")
   Call ("multipart/mixed")
   Set header = ("Content-Disposition")
   Call ("attachment; filename=" & Filename)
 
   Set header = ("Content-ID")
   Call (Filename)
  
   Set stream = ()
   If Not (strAttach, "binary") Then
    MsgBox "Open failed"
   End If
   If  = 0 Then
    MsgBox "File has no content"
   End If
   Call (stream, "application/octet-stream", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments.
  End If
  Next
  'Send the email
  Call (False) 
   = True ' Restore conversion
End Function
 VBA

But the reality is

We need to send emails from public mail

What is public mail: the email address used by the entire Team, such as ***admin@

Lotus notes, who have used anti-human Lotus notes, know that public mail needs to be opened first before entering

So when I added the above VBA code as follows and set the emails to send

  Server = "C***/****r/****"
  Path = "****\C*****.nsf"
  Set db = (Server, Path)

The email was indeed sent from public mail, but unfortunately, the email sender showed my personal email address. If you check my personal email, you can't find it at all, but you can see the email sent from public mail.

This is incomprehensible, so the long two-day human war against humans began.

I have tried various VBA codes before and after [Why don't the interface be adjusted directly]

But it can either be displayed as public mail, but the email body cannot be in the form of Html, otherwise it is the opposite. In short, in a word: you cannot have both.

During this period, I have seen all the websites about Lotus notes VBA at home and abroad

Finally, I really couldn't bear it anymore, so I started searching for Python, C#

I have been hesitating and not writing because my colleague told me that, for example, using C# requires an email password, and we don’t have this thing and will not have it.

Finally, I decided to take a gamble. I first used C# and wrote it out directly. When the error prompts that the password is not available, I will think of a solution.

So I trembled with the following code

/// <summary>
  /// Send emails via notes  /// </summary>
  /// <param name="mailTo">Real-time database</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static void SendForNotes()
  {
   string notesPwd = "";
   string notesServer = "C***3/C***/***r/***C";
   string NotesDBName = @"M**l\C***";
   string mailTo = "m****o@c**.***.com";
   string mailSubject = ();
   string mailBoby = "&lt;html&gt;&lt;body&gt;&lt;table border='1'&gt;&lt;tr&gt;&lt;th&gt;Month&lt;/th&gt;&lt;th&gt;Savings&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;January&lt;/td&gt;&lt;td&gt;$100&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;";
   NotesSession ns;
   NotesDatabase db;
   NotesDocument doc;
   try
   {
    ns = new NotesSession();
    if (ns != null)
    {
     //The password of your local notes     (notesPwd);
     //Initialize NotesDatabase     db = (notesServer, NotesDBName, false);
     doc = ();
     ("Form", "Memo");
     ("SendTo", mailTo);
     ("Subject", ('\r', ' ').Replace('\n', ' '));
     ("Principal", "C******m");//Set the sender nickname of the email     NotesRichTextItem rt = ("Body");
     var richStyle = ();
      = 1;
     (richStyle);
     (mailBoby);
     //Send email     object obj = ("SendTo");
     (false, ref obj);
     doc = null;
    }
   }
   catch (Exception ex)
   {
    // ();
   }
   finally
   {
    ns = null;
    db = null;
    doc = null;
   }
  }

With a mortality, I carefully clicked on the debugging

WTF!!!!

I actually received an email! No password! Don't you need a password? Passwords can be sent without using them! ! !

After trying again, I found that it was really not needed! ! !

Because we don’t need to enter a password when we turn on notes every day! ! ! This may be bound to the local ID file! ! ! You need to enter your password in your first company after graduation!

So happy

Start modifying the code

Final version

/// &lt;summary&gt;
  /// Send emails via notes  /// &lt;/summary&gt;
  /// <param name="mailTo">Real-time database/lysh</param>  /// &lt;returns&gt;&lt;/returns&gt;
  public static void SendForNotes2()
  {

   string notesPwd = "";
   string notesServer = "C****3/**/S***/****";
   string NotesDBName = @"****\******.nsf";
   string mailTo = "****t**@***.com";
   string mailSubject = ();

   string mailBoby = "&lt;html&gt;&lt;body&gt;&lt;table border='1'&gt;&lt;tr&gt;&lt;th&gt;Month&lt;/th&gt;&lt;th&gt;Savings&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;January&lt;/td&gt;&lt;td&gt;$100&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;";

   NotesSession ns;
   NotesDatabase db;
   NotesDocument doc;
   try
   {
    ns = new NotesSession();
    if (ns != null)
    {
     //The password of your local notes     (notesPwd);
     //Initialize NotesDatabase     db = (notesServer, NotesDBName, false);
     doc = ();
     ("Form", "Memo");
     ("SendTo", mailTo);
     ("Subject", ('\r', ' ').Replace('\n', ' '));

      = true;

     NotesStream HtmlBody = ();
     (mailBoby);//Build HTML emails and add company logos and system reminders at the beginning and end     NotesMIMEEntity mine = ("Body");//Build the email text     (HtmlBody, "text/html;charset=UTF-8", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);

     ("Principal", "C**********am");//Set the sender nickname of the email     //Send email     object obj = ("SendTo");
     (false, ref obj);
     doc = null;
    }
   }
   catch (Exception ex)
   {
    // ();
   }
   finally
   {
    ns = null;
    db = null;
    doc = null;
   }
  }

Also encountered during this period

Because the code is placed incorrectly, the display is incorrect

("Principal", "C**********am");//Set the sender nickname of the email

The moment I finally broke through, I felt really good. Although I still don’t know why I don’t have a password, I finally solved the problem of being confused for two days and dare not enjoy it alone.

Sometimes I just hear others say that this road cannot be done, so I will not walk.

Sometimes I just hear others say that it has been packaged, just adjust it directly, and I don’t know how to achieve it

Sometimes I just copy homework and think I can do it, so I don’t know when I use it.

I finally started to get less busy before the New Year. I owe so much, and it's time to make up for it slowly.

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.