当前位置: 移动技术网 > IT编程>开发语言>c# > C#中利用Lotus notes公共邮箱发送邮件的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

前言

公司的邮件系统用的是反人类的 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***/****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");//设置邮件的发件人昵称

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

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

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

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

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

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网