栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#中利用Lotus notes公共邮箱发送邮件的方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#中利用Lotus notes公共邮箱发送邮件的方法

前言

公司的邮件系统用的是反人类的 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 conversion
End Function
 VBA

但是现实情况是这样的

我们需要邮件从公邮发送出去

何谓公邮:整个Team使用的邮箱,如***admin@email.com 之类的邮箱

使用过反人类的 Lotus notes 都知道公邮是需要先打开个人邮箱才能进去的

于是当我把以上的VBA 代码增加如下代码,设置从公邮里面发送邮件后

  Server = "C******rS***/****";
   string NotesDBName = @"**********.nsf";
   string mailTo = "****t**@***.com";
   string mailSubject = DateTime.Now.ToString();

   string mailBoby = "
MonthSavings
January$100
"; 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");//设置邮件的发件人昵称

最终突破的那一刻心情真的很爽,虽然到到现在仍然不知道不要密码的原因,但总归解决了困惑两天的问题,不敢独享

有时候就是听别人说,这条路走不通,就不走了

有时候就是听别人说,已经封装好了,直接调吧,就调了而不知如何实现

有时候就是抄作业,以为自己会了,于是真真用的时候就不知道了 

年前终于开始不那么忙了,欠了那么多,该慢慢补回来了

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号