C#中如何利用Lotusnotes公共郵箱發(fā)送郵件

這篇文章主要介紹C#中如何利用Lotus notes公共郵箱發(fā)送郵件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)10年經(jīng)驗(yàn)成就非凡,專業(yè)從事網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,成都網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)頁(yè)制作,軟文發(fā)稿,廣告投放等。10年來(lái)已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:18982081108,我們期待您的來(lái)電!

前言

公司的郵件系統(tǒng)用的是反人類的 Lotus notes, 你敢信?

最近要實(shí)現(xiàn)一個(gè)功能,郵件提醒功能,就是通過(guò)自動(dòng)發(fā)送提醒郵件

 前前后后這個(gè)問(wèn)題搞了2天,由于公司的諸多條件限制,無(wú)法直接調(diào)用到公司發(fā)送郵件的接口,只有通過(guò)類似 Lotus script,VBA 等其他方式來(lái)實(shí)現(xiàn)。

用VBA代碼實(shí)現(xiàn)發(fā)送郵件,其實(shí)我在n年前就實(shí)現(xiàn)過(guò)了

代碼如下,網(wǎng)上一搜也一大堆

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 conversion
End Function
 VBA

但是現(xiàn)實(shí)情況是這樣的

我們需要郵件從公郵發(fā)送出去

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

使用過(guò)反人類的 Lotus notes 都知道公郵是需要先打開(kāi)個(gè)人郵箱才能進(jìn)去的

于是當(dāng)我把以上的VBA 代碼增加如下代碼,設(shè)置從公郵里面發(fā)送郵件后

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

郵件確實(shí)是從公郵發(fā)送出來(lái),但是很遺憾,郵件發(fā)送人那顯示的是我的個(gè)人郵箱,而查看我個(gè)人的已發(fā)送郵件,是完全查不到,但是在公郵已發(fā)送郵件可以看到

這就無(wú)法理解了,于是開(kāi)啟了漫長(zhǎng)的2天人類大戰(zhàn)反人類Lotus notes戰(zhàn)役

前前后后試過(guò)各種VBA代碼【表問(wèn)為什么不直接調(diào)接口】

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

期間看遍國(guó)內(nèi)外關(guān)于Lotus notes VBA的網(wǎng)站

最后,實(shí)在是忍不了了,開(kāi)始搜索Python,C#

一直猶猶豫豫沒(méi)有寫是因?yàn)橥赂嬖V我,比如使用C#就需要郵箱密碼,而這個(gè)東西我們沒(méi)有也不會(huì)有的

最后的最后,決定賭一把,我先用C#,直接寫出來(lái),等報(bào)錯(cuò)提示密碼沒(méi)有的時(shí)候我再想辦法

于是戰(zhàn)戰(zhàn)兢兢有了以下代碼

/// <summary>
  /// 通過(guò)notes發(fā)送郵件
  /// </summary>
  /// <param name="mailTo">實(shí)時(shí)數(shù)據(jù)庫(kù)</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)
    {
     //您本機(jī)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");//設(shè)置郵件的發(fā)件人昵稱
     NotesRichTextItem rt = doc.CreateRichTextItem("Body");
     var richStyle = ns.CreateRichTextStyle();
     richStyle.PassThruHTML = 1;
     rt.AppendStyle(richStyle);
     rt.AppendText(mailBoby);
     //發(fā)送郵件   
     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;
   }
  }

抱著必死的心態(tài)小心翼翼的點(diǎn)擊了調(diào)試

WTF!!!!

居然收到一封有郵件!沒(méi)有密碼??!不需要密碼嗎!密碼不用也能發(fā)送?。?!

再試了一次后,發(fā)現(xiàn)真的不需要?。?!

因?yàn)槲覀兠刻扉_(kāi)機(jī)打開(kāi)notes的時(shí)候也不需要輸入密碼?。?!這可能是和本機(jī)的ID文件有綁定?。?!在畢業(yè)后的第一家公司中是需要輸入密碼的!

于是欣喜若狂

開(kāi)始修改代碼

最終版本

/// <summary>
  /// 通過(guò)notes發(fā)送郵件
  /// </summary>
  /// <param name="mailTo">實(shí)時(shí)數(shù)據(jù)庫(kù)/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)
    {
     //您本機(jī)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);//構(gòu)建HTML郵件,可以在頭和尾添加公司的logo和系統(tǒng)提醒語(yǔ)
     NotesMIMEEntity mine = doc.CreateMIMEEntity("Body");//構(gòu)建郵件正文
     mine.SetContentFromText(HtmlBody, "text/html;charset=UTF-8", Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);

     doc.AppendItemValue("Principal", "C**********am");//設(shè)置郵件的發(fā)件人昵稱
     //發(fā)送郵件   
     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;
   }
  }

期間還遇到

由于這句代碼放置的位置不對(duì),導(dǎo)致顯示不正確

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

以上是“C#中如何利用Lotus notes公共郵箱發(fā)送郵件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站標(biāo)題:C#中如何利用Lotusnotes公共郵箱發(fā)送郵件
文章源于:http://bm7419.com/article12/igeidc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、品牌網(wǎng)站制作云服務(wù)器、面包屑導(dǎo)航、網(wǎng)站策劃電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)