C#中利用Lotus notes公共邮箱发送邮件的方法
作者:Lionel Andrés Messi 发布时间:2023-10-02 03:00:12
前言
公司的邮件系统用的是 * 的 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");//设置邮件的发件人昵称
最终突破的那一刻心情真的很爽,虽然到到现在仍然不知道不要密码的原因,但总归解决了困惑两天的问题,不敢独享
有时候就是听别人说,这条路走不通,就不走了
有时候就是听别人说,已经封装好了,直接调吧,就调了而不知如何实现
有时候就是抄作业,以为自己会了,于是真真用的时候就不知道了
年前终于开始不那么忙了,欠了那么多,该慢慢补回来了
来源:http://www.cnblogs.com/LionelMessi/p/8447879.html


猜你喜欢
- 在Java 8之前,对集合进行排序需要为排序中使用的比较器 Comparator 创建一个匿名内部类:new Compa
- 一、新建BeanUtil类import lombok.extern.slf4j.Slf4j;import org.springframewo
- Android:控件GridView的使用如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView
- 了解内存的原理1、内存是由 Key 和 Value 组成,Key 是内存地址、Value 是存储的数据;2、Key:是一个32位长度的二进制
- 在封装中有一种特殊的类,能够把基本的数据类型进行转换来方便实际的使用。我们在之前提到的一些数据类型,最明显的特征是所有字母为小写状态,那么经
- 这几年一直在做手机上和电视盒的App,几乎没有考虑过横竖屏切换的问题。电视盒好说,横屏不变,你要是给它设计个竖屏人家也没机会使;而手机上的应
- 前言Java克隆(Clone)是Java语言的特性之一,但在实际中应用比较少见。但有时候用克隆会更方便更有效率。对于克隆(Clone),Ja
- 最近在看 C++ 的方法重载,我就在想 C# 中的重载底层是怎么玩的,很多朋友应该知道 C 是不支持重载的,比如下面的代码就会报错。#inc
- 前言今天收到一封邮件,大概内容如下:spring boot鼓励去配置化,那么怎么将第三方jar包中的xml去配置化了?其实,这个问题,在前面
- 本文以一个asp.net程序为例讲述了Repeater中添加按钮实现点击按钮获取某一行数据的方法,分享给大家供大家参考借鉴之用。具体步骤如下
- 1、实例解析先从一个例子开始:public class LambdaTest { public static vo
- ImGUI 它是与平台无关的C++轻量级跨平台图形界面库,没有任何第三方依赖,可以将ImGUI的源码直接加到项目中使用,该框架通常会配合特定
- Android权限Android安全架构规定:默认情况下,任何应用都没有权限执行对其他应用、操作系统或用户有不利影响的任何操作。这包括读写用
- 本文实例为大家分享了RecyclerView实现滑动删除的具体代码,供大家参考,具体内容如下package com.example.demo
- mac版本:点击Finder,在应用程序中找到android studio----->Contents文件夹----->bin文
- Java 利用poi把数据库中数据导入Excel效果:使用时先把poi包导入工程的path,注意只需要导入poi包即可,下载后有三个jar包
- Redis是一个缓存消息中间件及具有丰富特性的键值存储系统。Spring Boot为Jedis客户端库和由Spring Data Redis
- 一、Stream流简单示例需求:按照要求集合创建和遍历创建一个结合,存储多个字符串元素把集合中所有以"张"开头的元素存储
- 本文实例讲述了C#使用NPOI导入Excel的方法。分享给大家供大家参考,具体如下:NPOI是由国人开发的一个进行excel操作的第三方库。
- 一次性全部绘制出来实现代码import java.awt.*;public class AlgoVisualizer {private st