a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

C#中利用Lotus notes公共郵箱發送郵件的方法

2019-10-29 21:02:46
字體:
來源:轉載
供稿:網友

前言

公司的郵件系統用的是反人類的 Lotus notes, 你敢信?

最近要實現一個功能,郵件提醒功能,就是通過自動發送提醒郵件

 前前后后這個問題搞了2天,由于公司的諸多條件限制,無法直接調用到公司發送郵件的接口,只有通過類似 Lotus script,VBA 等其他方式來實現。

用VBA代碼實現發送郵件,其實我在n年前就實現過了

代碼如下,網上一搜也一大堆

Function SendEmailbyNotesWithAttachement_2(Addresses, Attach, cc) strSubject = ThisWorkbook.Sheets("EMAIL").Range("B1") strbody = ThisWorkbook.Sheets("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("Notes.NotesSession")  Set db = s.CURRENTDATABASE  Set stream = s.CreateStream  ' Turn off auto conversion to rtf  s.ConvertMIME = False  ' Create message  Set message = db.CREATEDOCUMENT  message.Form = "memo"  message.Subject = strSubject  message.sendTo = Split(Addresses, ";")  message.CopyTo = cc  message.SaveMessageOnSend = True  ' Create the body to hold HTML and attachment  Set body = message.CreateMIMEEntity 'Child mime entity which is going to contain the HTML which we put in the stream  Set bodyChild = body.CreateChildEntity()  Call stream.WriteText(strbody)  Call bodyChild.SetContentFromText(stream, "text/HTML;charset=UTF-8", ENC_NONE)  Call stream.Close  Call stream.Truncate  ' 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 = body.CreateChildEntity()   Set header = bodyChild.CreateHeader("Content-Type")   Call header.SetHeaderVal("multipart/mixed")   Set header = bodyChild.CreateHeader("Content-Disposition")   Call header.SetHeaderVal("attachment; filename=" & Filename)    Set header = bodyChild.CreateHeader("Content-ID")   Call header.SetHeaderVal(Filename)     Set stream = s.CreateStream()   If Not stream.Open(strAttach, "binary") Then    MsgBox "Open failed"   End If   If stream.Bytes = 0 Then    MsgBox "File has no content"   End If   Call bodyChild.SetContentFromBytes(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 message.Send(False)   s.ConvertMIME = True ' Restore conversionEnd Function VBA

但是現實情況是這樣的

我們需要郵件從公郵發送出去

何謂公郵:整個Team使用的郵箱,如***admin@email.com 之類的郵箱

使用過反人類的 Lotus notes 都知道公郵是需要先打開個人郵箱才能進去的

于是當我把以上的VBA 代碼增加如下代碼,設置從公郵里面發送郵件后

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

郵件確實是從公郵發送出來,但是很遺憾,郵件發送人那顯示的是我的個人郵箱,而查看我個人的已發送郵件,是完全查不到,但是在公郵已發送郵件可以看到

這就無法理解了,于是開啟了漫長的2天人類大戰反人類Lotus notes戰役

前前后后試過各種VBA代碼【表問為什么不直接調接口】

但要不就是能顯示為公郵發送的,但郵件 body 不能Html格式,否則就是相反,總之一句話:二者不可兼得

期間看遍國內外關于Lotus notes VBA的網站

最后,實在是忍不了了,開始搜索Python,C#

一直猶猶豫豫沒有寫是因為同事告訴我,比如使用C#就需要郵箱密碼,而這個東西我們沒有也不會有的

最后的最后,決定賭一把,我先用C#,直接寫出來,等報錯提示密碼沒有的時候我再想辦法

于是戰戰兢兢有了以下代碼

/// <summary>  /// 通過notes發送郵件  /// </summary>  /// <param name="mailTo">實時數據庫</param>  /// <returns></returns>  public static void SendForNotes()  {   string notesPwd = "";   string notesServer = "C***3/C***/***r/***C";   string NotesDBName = @"M**l/C***to.nsf";   string mailTo = "m****o@c**.***.com";   string mailSubject = DateTime.Now.ToString();   string mailBoby = "<html><body><table border='1'><tr><th>Month</th><th>Savings</th></tr><tr><td>January</td><td>$100</td></tr></table></body></html>";   NotesSession ns;   NotesDatabase db;   NotesDocument doc;   try   {    ns = new NotesSession();    if (ns != null)    {     //您本機notes的密碼     ns.Initialize(notesPwd);     //初始化NotesDatabase     db = ns.GetDatabase(notesServer, NotesDBName, false);     doc = db.CreateDocument();     doc.ReplaceItemValue("Form", "Memo");     doc.ReplaceItemValue("SendTo", mailTo);     doc.ReplaceItemValue("Subject", mailSubject.Replace('/r', ' ').Replace('/n', ' '));     doc.AppendItemValue("Principal", "C******m");//設置郵件的發件人昵稱     NotesRichTextItem rt = doc.CreateRichTextItem("Body");     var richStyle = ns.CreateRichTextStyle();     richStyle.PassThruHTML = 1;     rt.AppendStyle(richStyle);     rt.AppendText(mailBoby);     //發送郵件        object obj = doc.GetItemValue("SendTo");     doc.Send(false, ref obj);     doc = null;    }   }   catch (Exception ex)   {    // Log.CreateLog(ex.Message);   }   finally   {    ns = null;    db = null;    doc = null;   }  }

抱著必死的心態小心翼翼的點擊了調試

WTF!!!!

居然收到一封有郵件!沒有密碼啊!不需要密碼嗎!密碼不用也能發送!!!

再試了一次后,發現真的不需要!!!

因為我們每天開機打開notes的時候也不需要輸入密碼!!!這可能是和本機的ID文件有綁定!!!在畢業后的第一家公司中是需要輸入密碼的!

于是欣喜若狂

開始修改代碼

最終版本

/// <summary>  /// 通過notes發送郵件  /// </summary>  /// <param name="mailTo">實時數據庫/lysh</param>  /// <returns></returns>  public static void SendForNotes2()  {   string notesPwd = "";   string notesServer = "C****3/**/S***/****";   string NotesDBName = @"****/******.nsf";   string mailTo = "****t**@***.com";   string mailSubject = DateTime.Now.ToString();   string mailBoby = "<html><body><table border='1'><tr><th>Month</th><th>Savings</th></tr><tr><td>January</td><td>$100</td></tr></table></body></html>";   NotesSession ns;   NotesDatabase db;   NotesDocument doc;   try   {    ns = new NotesSession();    if (ns != null)    {     //您本機notes的密碼     ns.Initialize(notesPwd);     //初始化NotesDatabase     db = ns.GetDatabase(notesServer, NotesDBName, false);     doc = db.CreateDocument();     doc.ReplaceItemValue("Form", "Memo");     doc.ReplaceItemValue("SendTo", mailTo);     doc.ReplaceItemValue("Subject", mailSubject.Replace('/r', ' ').Replace('/n', ' '));     doc.SaveMessageOnSend = true;     NotesStream HtmlBody = ns.CreateStream();     HtmlBody.WriteText(mailBoby);//構建HTML郵件,可以在頭和尾添加公司的logo和系統提醒語     NotesMIMEEntity mine = doc.CreateMIMEEntity("Body");//構建郵件正文     mine.SetContentFromText(HtmlBody, "text/html;charset=UTF-8", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);     doc.AppendItemValue("Principal", "C**********am");//設置郵件的發件人昵稱     //發送郵件        object obj = doc.GetItemValue("SendTo");     doc.Send(false, ref obj);     doc = null;    }   }   catch (Exception ex)   {    // Log.CreateLog(ex.Message);   }   finally   {    ns = null;    db = null;    doc = null;   }  }

期間還遇到

由于這句代碼放置的位置不對,導致顯示不正確

doc.AppendItemValue("Principal", "C**********am");//設置郵件的發件人昵稱

最終突破的那一刻心情真的很爽,雖然到到現在仍然不知道不要密碼的原因,但總歸解決了困惑兩天的問題,不敢獨享

有時候就是聽別人說,這條路走不通,就不走了

有時候就是聽別人說,已經封裝好了,直接調吧,就調了而不知如何實現

有時候就是抄作業,以為自己會了,于是真真用的時候就不知道了 

年前終于開始不那么忙了,欠了那么多,該慢慢補回來了

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: xxxx性欧美 | 国产不卡在线观看 | 日本中文字幕免费 | 777xacom| 日本午夜精品 | 色网站免费视频 | 无码一区二区三区视频 | 国产成人久久精品一区二区三区 | 99av| 亚洲首页 | 国产精品二区三区 | 欧美一极片| 91文字幕巨乱亚洲香蕉 | 国产免费一区二区三区网站免费 | 蜜臀99久久精品久久久久久软件 | 国产午夜精品一区二区三区嫩草 | 日本中文字幕在线 | 激情亚洲婷婷 | 色综合天天综合网天天看片 | 一区二区三区四区精品 | 蜜桃av中文字幕 | 日本视频一区二区三区 | 嫩草网站在线观看 | a√免费视频 | 亚洲福利av | 99免费看| 91精品久久久久久久久久 | 日本精品免费 | 午夜精品一区二区三区在线观看 | 亚洲精品电影在线观看 | 成人精品鲁一区一区二区 | 亚洲第一成年免费网站 | 国产乱码精品一区二区三区忘忧草 | 日韩伦理一区二区三区 | 国产精品一区在线观看你懂的 | 超碰在线人 | 欧美久久久久久久久久伊人 | 欧美精品在线一区二区三区 | 成人蜜桃视频 | 亚洲精品久久久久久一区二区 | 中文日韩在线 |