用c# 和 Lotus Domino Objects Com 发送HTML格式邮件

.Net技术 码拜 10年前 (2015-02-11) 1511次浏览 0个评论

1.本机安装Lotus Notes客户端。
2.注册组件:regsvr32 “C:\Program Files\lotus\notes\nlsxbe.dll”。
3.打开vs2010,新建ASP.NET Web应用程序,取名“NotesMail”。
4.添加引用,选 “com” 找到“Lotus Domino Objects”,点 “确定”。
5.新建文件夹 “EmailTemplate”,新建 HTML页,取名 “DefaultTemplate.htm”,在里写html代码。例如:
1:
2:
3:
4:
5:
6:
7: Dear {0},
8: 您好!

9:

{1}
10: 请点击连接:報修單查詢

11:

此為系統郵件,請勿回覆!
12:
13:
6.新建一个类 “EmailTemplateGenerate.cs”用于读取文件
1: using System;
2: using System.Web;
3: using System.IO;
4:
5: namespace NotesMail
6: {
7: public class EmailTempGenerate
8: {
9: public string GetTemplate(string path)
10: {
11: path = HttpContext.Current.Server.MapPath(path);
12: using (StreamReader sr = new StreamReader(path))
13: {
14: try
15: {
16: string result = sr.ReadToEnd();
17: return result;
18: }
19: catch (Exception err)
20: {
21: throw new Exception(err.Message);
22: }
23: }
24: }
25: }
26: }
7.新建一个类 “Common”,再建一个静态方法 “AppPath” 用于读取IP/主机名+应用程序根路径
1: using System.Web;
2:
3: namespace NotesMail
4: {
5: public class Common
6: {
7: public static string AppPath
8: {
9: get
10: {
11: return HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
12: }
13: }
14: }
15: }
8.为测试,在 Default.aspx上放置一个 Button,起名为 “btn_Send”,双击添加click事件
9.打开 “Default.aspx.cs” ,编写代码,实现发送HTML邮件
步骤1:添加引用 using Domino;
步骤2:定义
1: NotesSession ns;
2: NotesDatabase db;
3: NotesDocument doc;
步骤3:写“btn_Send_Click”
1: protected void btn_Send_Click(object sender, EventArgs e)
2: {
3: try
4: {
5: ns = new NotesSession();
6: if (ns != null)
7: {
8: //您本机notes的密码
9: ns.Initialize(“password”);
10: //初始化NotesDatabase
11: db = ns.GetDatabase(“Domino服务器名称”, “names.nsf”, false);
12: doc = db.CreateDocument();
13: doc.ReplaceItemValue(“Form”, “Memo”);
14: doc.ReplaceItemValue(“SendTo”, “Notes用户名”);
15: doc.ReplaceItemValue(“Subject”, “通知邮件“);
16: //html内容的邮件
17: ns.ConvertMime = false;
18: EmailTempGenerate etg = new EmailTempGenerate();
19: string temp = etg.GetTemplate(ResolveUrl(“~/EmailTemplate/DefaultTemplate.htm”));
20: string htmlbody = string.Format(temp, “尊敬的用户”, “您的物品维修完成”, Common.AppPath);
21: NotesStream stream = ns.CreateStream();
22: stream.WriteText(htmlbody, EOL_TYPE.EOL_ANY);
23: NotesMIMEEntity mimebody = doc.CreateMIMEEntity(“Body”);
24: NotesMIMEEntity mimehtml = mimebody.CreateChildEntity(null);
25: mimehtml.SetContentFromText(stream, “text/html; charset=UTF-8”, MIME_ENCODING.ENC_NONE);
26: stream.Truncate();
27: stream.Close();
28: doc.CloseMIMEEntities(true, “Body”);
29: ns.ConvertMime = true;
30: doc.ComputeWithForm(false, false);
31: doc.Send(false);
32: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), “”, “alert(“Message Send”);”, true);
33: }
34: }
35: catch (Exception ex)
36: {
37: string msg = “alert(“” + ex.Message + “”);”;
38: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), “”, msg, true);
39: }
40: finally
41: {
42: ns = null;
43: db = null;
44: doc = null;
45: }
46: }
如果想获取notes用户的信息,如用户名,internet地址等,方便起见,页面上添加一个listbox用于显示,名称为: “lbx_to” 方法如下:
1: NotesSession ns;
2: NotesDatabase db;
3: NotesDocument doc;
4: NotesView vw;
5:
6: protected void Page_Load(object sender, EventArgs e)
7: {
8: if (!IsPostBack)
9: {
10: init();
11: }
12: }
13: void init()
14: {
15: try
16: {
17: ns = new NotesSession();
18: ns.Initialize(“eola”);
19: db = ns.GetDatabase(“Domino服务器名称”, “names.nsf”, false);
20: if (db != null)
21: {
22: vw = db.GetView(“People”);
23: doc = vw.GetFirstDocument();
24: while (doc != null)
25: {
26: var firstname = doc.GetFirstItem(“FirstName”);
27: var lastname = doc.GetFirstItem(“LastName”);
28: var InternetAddress = doc.GetFirstItem(“InternetAddress”);
29: if (firstname != null && lastname != null && InternetAddress != null)
30: {
31: string temp = InternetAddress.Values[0] as string;
32: if (!string.IsNullOrWhiteSpace(temp))
33: {
34: lbx_to.Items.Add(“用户名:”+lastname.Text + ” 邮件地址:” + temp.Trim());
35: }
36: }
37: doc = vw.GetNextDocument(doc);
38: }
39: }
40: }
41: catch (Exception ex)
42: {
43: return;
44: }
45: finally
46: {
47: ns = null;
48: doc = null;
49: db = null;
50: }
51:
52: }


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用c# 和 Lotus Domino Objects Com 发送HTML格式邮件
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!