Heim > php教程 > php手册 > C#WindowsService调用IBMLotusNotes发送邮件

C#WindowsService调用IBMLotusNotes发送邮件

WBOY
Freigeben: 2016-07-06 13:30:22
Original
1188 Leute haben es durchsucht

近日研究了下 IBMLotusMail ,这货果然是麻烦,由于公司策略,没有开放 smtp ,很多系统邮件都没有办法发送,于是入手 google 学习 LotusMail ,想做成 Windows 服务,提供 wcf 服务给内部应用系统使用。在 google 上找了很多资料,由于是系统邮件,很多东西

近日研究了下IBM Lotus Mail,这货果然是麻烦,由于公司策略,没有开放smtp,很多系统邮件都没有办法发送,于是入手google学习Lotus Mail,想做成Windows服务,提供wcf服务给内部应用系统使用。在google上找了很多资料,由于是系统邮件,很多东西配置起来又比较麻烦。自己也入了很多坑,特此作为记录。废话不多说,下面开始...

服务器环境:Windows Server 2008R2+Lotus Notes 8.5中文版

特别注意:Lotus Notes 8.5中文版需要配置好账户密码,但是不需要打开它。

本地环境:Lotus Notes 8.5中文版+Visual Studio 2013

~~~~~~~~~~~~~~~~~~~~~~~我是优雅的分隔符~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1,打开VS,新建类库项目LotusMailHelper,添加Lotus Domino Objects引用:

添加完之后VS会自动编译成dll

2,添加类Mail.cs,添加邮件发送方法SendMail

<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>

<span style="color: #808080;">///</span><span style="color: #008000;"> 发送邮件

</span><span style="color: #808080;">///</span> <span style="color: #808080;"></span>

<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="sendTo"></span>

<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="subject"></span>

<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="messageBody"></span>

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> SendMail(<span style="color: #0000ff;">string</span>[] sendTo, <span style="color: #0000ff;">string</span> subject, <span style="color: #0000ff;">string</span> messageBody)
Nach dem Login kopieren

3,在Mail.cs添加Using:using Domino;

4,编写SendMail的逻辑

Domino.Notessession nSession = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Domino.NotesSession();
</span><span style="color: #0000ff;">string</span> pwd = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailPassWord</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">lotus邮箱密码</span>
<span style="color: #0000ff;">string</span> server = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailServer</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">lotus邮箱服务器地址</span>
<span style="color: #0000ff;">string</span> serverPath = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailServerPath</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">存储nsf文件的路径</span>
<span style="color: #0000ff;">string</span> saveMessageOnSend = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">SaveMessageOnSend</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">发送前是否保存</span>
nSession.Initialize(pwd);<span style="color: #008000;">//</span><span style="color: #008000;">初始化邮件</span>
Domino.NotesDatabase nDatabase =<span style="color: #000000;">
nSession.GetDatabase(server, serverPath, </span><span style="color: #0000ff;">false</span><span style="color: #000000;">);
Domino.NotesDocument nDocument </span>=<span style="color: #000000;"> nDatabase.CreateDocument();
nDocument.ReplaceItemValue(</span><span style="color: #800000;">"</span><span style="color: #800000;">SentTo</span><span style="color: #800000;">"</span>, sendTo);<span style="color: #008000;">//</span><span style="color: #008000;">收件人,数据:数组</span>
nDocument.ReplaceItemValue(<span style="color: #800000;">"</span><span style="color: #800000;">Subject</span><span style="color: #800000;">"</span>, subject);<span style="color: #008000;">//</span><span style="color: #008000;">主题</span>
<span style="color: #0000ff;">if</span> (saveMessageOnSend == <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">为1时保存到lotus的发件箱</span>
<span style="color: #000000;">{
    nDocument.SaveMessageOnSend </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">
{
    nDocument.SaveMessageOnSend </span>= <span style="color: #0000ff;">false</span>;<span style="color: #008000;">//</span><span style="color: #008000;">设置保存与否</span>
<span style="color: #000000;">}
NotesStream HtmlBody </span>=<span style="color: #000000;"> nSession.CreateStream();
HtmlBody.WriteText(messageBody);</span><span style="color: #008000;">//</span><span style="color: #008000;">构建HTML邮件,可以在头和尾添加公司的logo和系统提醒语</span>
NotesMIMEEntity mine = nDocument.CreateMIMEEntity(<span style="color: #800000;">"</span><span style="color: #800000;">Body</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">构建邮件正文</span>
mine.SetContentFromText(HtmlBody, <span style="color: #800000;">"</span><span style="color: #800000;">text/html;charset=UTF-8</span><span style="color: #800000;">"</span><span style="color: #000000;">, Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);
nDocument.AppendItemValue(</span><span style="color: #800000;">"</span><span style="color: #800000;">PRincipal</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">XXX管理员</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">设置邮件的发件人昵称</span>
nDocument.Send(<span style="color: #0000ff;">false</span>, sendTo); <span style="color: #008000;">//</span><span style="color: #008000;">发送邮件</span>
nDocument.CloseMIMEEntities();<span style="color: #008000;">//</span><span style="color: #008000;">关闭</span>
Nach dem Login kopieren

由于最后会封装为dll,最好是添加try...catch...,加以优化,下面为优化过后:

<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>
<span style="color: #808080;">///</span><span style="color: #008000;"> 發送lotus郵件(需要在web.config或者app.config中添加以下節點
</span><span style="color: #808080;">///</span><span style="color: #808080;"><appsettings></appsettings></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><!--郵箱密碼--></span>
<span style="color: #808080;">///</span><span style="color: #808080;"><add key="LotusMailPassword" value=""></add></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><!--郵件服務器地址--></span>
<span style="color: #808080;">///</span><span style="color: #808080;"><add key="LotusMailServer" value=""></add></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><!--郵件數據庫路徑--></span>
<span style="color: #808080;">///</span><span style="color: #808080;"><add key="LotusMailServerPath" value=""></add></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><!--是否保存到發件箱(0不保存,1保存,其他值皆為不保存)--></span>
<span style="color: #808080;">///</span><span style="color: #808080;"><add key="SaveMessageOnSend" value="0"></add></span>
<span style="color: #808080;">///</span><span style="color: #808080;"></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="sendTo"></span><span style="color: #008000;">數組,收件人</span><span style="color: #808080;"></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="subject"></span><span style="color: #008000;">主題</span><span style="color: #808080;"></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="messageBody"></span><span style="color: #008000;">正文html</span><span style="color: #808080;"></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><returns></returns></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> SendMail(<span style="color: #0000ff;">string</span>[] sendTo, <span style="color: #0000ff;">string</span> subject, <span style="color: #0000ff;">string</span><span style="color: #000000;"> messageBody)
{
    </span><span style="color: #0000ff;">try</span><span style="color: #000000;">
    {
        Domino.NotesSession nSession </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Domino.NotesSession();
        </span><span style="color: #0000ff;">string</span> pwd = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailPassword</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">lotus邮箱密码</span>
        <span style="color: #0000ff;">string</span> server = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailServer</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">lotus邮箱服务器地址</span>
        <span style="color: #0000ff;">string</span> serverPath = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">LotusMailServerPath</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">存储nsf文件的路径</span>
        <span style="color: #0000ff;">string</span> saveMessageOnSend = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">SaveMessageOnSend</span><span style="color: #800000;">"</span>];<span style="color: #008000;">//</span><span style="color: #008000;">发送前是否保存</span>
        nSession.Initialize(pwd);<span style="color: #008000;">//</span><span style="color: #008000;">初始化邮件</span>
        Domino.NotesDatabase nDatabase =<span style="color: #000000;">
        nSession.GetDatabase(server, serverPath, </span><span style="color: #0000ff;">false</span><span style="color: #000000;">);
        Domino.NotesDocument nDocument </span>=<span style="color: #000000;"> nDatabase.CreateDocument();
        nDocument.ReplaceItemValue(</span><span style="color: #800000;">"</span><span style="color: #800000;">SentTo</span><span style="color: #800000;">"</span>, sendTo);<span style="color: #008000;">//</span><span style="color: #008000;">收件人,数据:数组</span>
        nDocument.ReplaceItemValue(<span style="color: #800000;">"</span><span style="color: #800000;">Subject</span><span style="color: #800000;">"</span>, subject);<span style="color: #008000;">//</span><span style="color: #008000;">主题</span>
        <span style="color: #0000ff;">if</span> (saveMessageOnSend == <span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span>)<span style="color: #008000;">//</span><span style="color: #008000;">为1时保存到lotus的发件箱</span>
<span style="color: #000000;">        {
            nDocument.SaveMessageOnSend </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            nDocument.SaveMessageOnSend </span>= <span style="color: #0000ff;">false</span>;<span style="color: #008000;">//</span><span style="color: #008000;">设置保存与否</span>
<span style="color: #000000;">        }
        NotesStream HtmlBody </span>=<span style="color: #000000;"> nSession.CreateStream();
        HtmlBody.WriteText(messageBody);</span><span style="color: #008000;">//</span><span style="color: #008000;">构建HTML邮件,可以在头和尾添加公司的logo和系统提醒语</span>
        NotesMIMEEntity mine = nDocument.CreateMIMEEntity(<span style="color: #800000;">"</span><span style="color: #800000;">Body</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">构建邮件正文</span>
        mine.SetContentFromText(HtmlBody, <span style="color: #800000;">"</span><span style="color: #800000;">text/html;charset=UTF-8</span><span style="color: #800000;">"</span><span style="color: #000000;">, Domino.MIME_ENCODING.ENC_IDENTITY_BINARY);
        nDocument.AppendItemValue(</span><span style="color: #800000;">"</span><span style="color: #800000;">Principal</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">XXX管理员</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">设置邮件的发件人昵称</span>
        nDocument.Send(<span style="color: #0000ff;">false</span>, sendTo); <span style="color: #008000;">//</span><span style="color: #008000;">发送邮件</span>
        nDocument.CloseMIMEEntities();<span style="color: #008000;">//</span><span style="color: #008000;">关闭</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>;<span style="color: #008000;">//</span><span style="color: #008000;">已经提交到lotus,返回true</span>
<span style="color: #000000;">    }
    </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span>;<span style="color: #008000;">//</span><span style="color: #008000;">提交失败</span>
<span style="color: #000000;">    }
}</span>
Nach dem Login kopieren

5,点击项目生成,找到Bin文件夹中的dll,保存到自己喜欢的文件夹,方便后期的调用

============我是更加优美的分隔符=============

下面一起来建立Windows service

1,打开VS,新建Windows服务项目

 名字随便取。。。新建完成之后会自动生成Service1.cs,打开Service1.cs代码看看,主要分为以下几个方法:

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Service1 : ServiceBase
{
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Service1()
    {
        InitializeComponent();
    }
    
    </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnStart(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
    {
    }
    
    </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnStop()
    {
    }
}</span>
Nach dem Login kopieren
OnStart:主要是写入要启动的逻辑代码
Nach dem Login kopieren
<span>OnStop:主要写的是停止服务时要执行的方法,也就是逻辑代码,我一般会将日志写在这<br></span>
Nach dem Login kopieren

2,将service1.cs删除,新建一个Windows服务,并命名成公司要求的。例如我的是MailService.cs。

3,新建一个处理邮件的方法:

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> SendMail()
{
    </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">)
    {
        </span><span style="color: #008000;">//</span><span style="color: #008000;">这里写邮件数据获取以及发送邮件</span>
        Thread.Sleep(<span style="color: #800080;">100</span><span style="color: #000000;">);
    }   
}</span>
Nach dem Login kopieren

4,构建邮件model:在解决方案点击鼠标右键添加新建项目,选择类库项目,MailModel,新建MailInfo.cs

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MailInfo
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> mailId { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span>[] sendTo { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> subject { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> mailBody { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
}</span>
Nach dem Login kopieren

5,新建类库DbHelper,添加类Mail.cs,在里面写GetMailData()方法,RemoveMailData(),GetMailCount(),InsertMailData()等方法,这里由于涉及到公司的信息,不是很方便写出来。大家可以自行添加进去

<span style="color: #0000ff;">public</span><span style="color: #000000;"> MailModel.MailInfo GetMailData()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">这里写获取邮件数据</span>
    <span style="color: #0000ff;">return</span> MailInfo;<span style="color: #008000;">//</span><span style="color: #008000;">返回数据库第一封待发邮件数据</span>
<span style="color: #000000;">}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> RemoveMailData(<span style="color: #0000ff;">string</span><span style="color: #000000;"> mailId)
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">删除数据库中指定id的邮件数据</span>
<span style="color: #000000;">}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> GetMailCount()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">这里写获取邮件数量</span>
    <span style="color: #0000ff;">return</span><span style="color: #000000;"> 邮件数量
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> InsertMailData()
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">这里写插入一封邮件数据</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}</span>
Nach dem Login kopieren

6,新建类库WCF项目,添加wcf,名字为SendMail

添加完成之后VS会自动生成ISendMail.cs和SendMail.cs。打开ISendMail.cs会看到如下代码

<span style="color: #008000;">//</span><span style="color: #008000;"> 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ISendMail”。</span>
<span style="color: #000000;">[ServiceContract]
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> ISendMail
{
    [OperationContract]
    </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> DoWork();
}</span>
Nach dem Login kopieren

里面只有一个DoWork方法,我们新建一个ApplySendMail();注意:在顶上要添加[OperationContract]否则不会公开该函数。最后的ISendMail.cs代码如下

<span style="color: #000000;">[ServiceContract]
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> ISendMail
{
    [OperationContract]
    </span><span style="color: #0000ff;">string</span> ApplySendMail(<span style="color: #0000ff;">string</span>[] sendTo, <span style="color: #0000ff;">string</span> subject, <span style="color: #0000ff;">string</span> body, <span style="color: #0000ff;">string</span><span style="color: #000000;"> password);
}</span>
Nach dem Login kopieren

接着打开SendMail.cs,去实现接口的ApplySendMail()方法

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SendMail : ISendMail
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> ApplySendMail(<span style="color: #0000ff;">string</span>[] sendTo, <span style="color: #0000ff;">string</span> subject, <span style="color: #0000ff;">string</span> body, <span style="color: #0000ff;">string</span><span style="color: #000000;"> password)
    {
        </span><span style="color: #0000ff;">string</span> result = <span style="color: #0000ff;">string</span><span style="color: #000000;">.Empty;
        </span><span style="color: #0000ff;">string</span> mailPassword = System.Configuration.ConfigurationManager.AppSettings[<span style="color: #800000;">"</span><span style="color: #800000;">password</span><span style="color: #800000;">"</span><span style="color: #000000;">];
        </span><span style="color: #0000ff;">if</span> (mailPassword ==<span style="color: #000000;"> password)
        {
            </span><span style="color: #0000ff;">try</span><span style="color: #000000;">
            {
                MailModel.MailInfo mail </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> MailModel.MailInfo
                {
                    sendTo </span>=<span style="color: #000000;"> sendTo,
                    subject </span>=<span style="color: #000000;"> subject,
                    mailBody </span>=<span style="color: #000000;"> body
                };
                </span><span style="color: #0000ff;">long</span> count =<span style="color: #000000;"> DbHelper.Mail.GetMailCount();
                </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (DbHelper.Mail.InsertMailData(mail))
                {
                    result </span>= <span style="color: #0000ff;">string</span>.Format(<span style="color: #800000;">"</span><span style="color: #800000;">提交成功.前面大約還有:{0}個任務</span><span style="color: #800000;">"</span><span style="color: #000000;">, count);
                }
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result;
            }
            </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">
            {
                </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">提交失敗</span><span style="color: #800000;">"</span><span style="color: #000000;">;
            }
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">密码错误,无法提交</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        }
    }
}</span>
Nach dem Login kopieren

至此wcf基础已经可以了,下面继续完成Windows服务那一块

7,完成处理邮件的方法SendMail(),这里要添加之前写好的LoutusMailHelper.dll

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> SendMail()
{
    </span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">var</span> mailData=<span style="color: #000000;">DbHelper.Mail.GetMailData();
        </span><span style="color: #0000ff;">if</span>(mailData!=<span style="color: #0000ff;">null</span><span style="color: #000000;">)
        {
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(LotusMailHelper.Mail.SendMail(mailData.sendTo,mailData.subject,mailData.mailBody))
            {
                DbHelper.Mail.RemoveMailData(mailData.mailId);
            }
            Thread.Sleep(</span><span style="color: #800080;">100</span>);<span style="color: #008000;">//</span><span style="color: #008000;">休息0.1秒</span>
<span style="color: #000000;">        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            Thread.Sleep(</span><span style="color: #800080;">10000</span>);<span style="color: #008000;">//</span><span style="color: #008000;">休息10秒钟</span>
<span style="color: #000000;">        }       
    }   
}</span>
Nach dem Login kopieren

8,完成OnStart()逻辑:①,先添加私有成员到MailService.cs

<span style="color: #0000ff;">partial</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MailService : ServiceBase
{
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> MailService()
    {
        InitializeComponent();
    }
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> System.ServiceModel.ServiceHost _host;
    
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">
    此处省略部分代码
    </span><span style="color: #008000;">*/</span><span style="color: #000000;">
}</span>
Nach dem Login kopieren

②,编写OnStart()代码

<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnStart(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
{
    _host </span>= <span style="color: #0000ff;">new</span> System.ServiceModel.ServiceHost(<span style="color: #0000ff;">typeof</span><span style="color: #000000;">(WCF.Mail));
    _host.Open();
    </span><span style="color: #008000;">//</span><span style="color: #008000;">启动wcf服务
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">启动一个线程专门轮询发送邮件</span>
    Thread sendMail = <span style="color: #0000ff;">new</span> Thread(<span style="color: #0000ff;">new</span><span style="color: #000000;"> ThreadStart(SendMail));
    sendMail.IsBackground </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    sendMail.Start();
}</span>
Nach dem Login kopieren

9,编写OnStop()代码,添加日志记录代码

10,配置App.config,wcf一定要配置。先看App.config中是否存在system.serviceModel节点,存在的话只需修改部分字段即可,不存在的话添加如下:

<system.servicemodel>
    <behaviors>
      <servicebehaviors>
        <behavior name="<span" style="color: #800000;">"">
          <servicemetadata httpgetenabled="<span" style="color: #800000;">"<span style="color: #800000;">true</span><span style="color: #800000;">"</span> httpsGetEnabled=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> />
          <servicedebug includeexceptiondetailinfaults="<span" style="color: #800000;">"<span style="color: #800000;">false</span><span style="color: #800000;">"</span> />
        </servicedebug></servicemetadata></behavior>
      </servicebehaviors>
    </behaviors>
    <services>
      <service name="<span" style="color: #800000;">"<span style="color: #800000;">WCF.Mail</span><span style="color: #800000;">"</span>>
        <endpoint address="<span" style="color: #800000;">"" binding=<span style="color: #800000;">"</span><span style="color: #800000;">basicHttpBinding</span><span style="color: #800000;">"</span> contract=<span style="color: #800000;">"</span><span style="color: #800000;">WCF.IMail</span><span style="color: #800000;">"</span>>
          <identity>
            <dns value="<span" style="color: #800000;">"<span style="color: #800000;">localhost</span><span style="color: #800000;">"</span> />
          </dns></identity>
        </endpoint>
        <endpoint address="<span" style="color: #800000;">"<span style="color: #800000;">mex</span><span style="color: #800000;">"</span> binding=<span style="color: #800000;">"</span><span style="color: #800000;">mexHttpBinding</span><span style="color: #800000;">"</span> contract=<span style="color: #800000;">"</span><span style="color: #800000;">IMetadataExchange</span><span style="color: #800000;">"</span> />
        <!--wcf节点配置开始-->
        <host>
          <baseaddresses>
            <add baseaddress="<span" style="color: #800000;">"<span style="color: #800000;">http://localhost:8733/MailService/Mail/</span><span style="color: #800000;">"</span> />
          </add></baseaddresses>
        </host>
        <!--wcf节点配置结束-->
      </endpoint></service>
    </services>
  </system.servicemodel>
Nach dem Login kopieren

~~至此,基本的都已经完成,下面到Windows service部署

1,打开MailService.cs视图界面,添加安装程序。会自动出现如下界面:

选中serviceProcessInstaller1组件,查看属性,设置account为LocalSystem

选中serviceInstaller1组件,查看属性

设置ServiceName的值, 该值表示在系统服务中的名称

设置StartType, 如果为Manual则手动启动,默认停止,如果为Automatic为自动启动

设置Description,添加服务描述

2,重新生成项目

3,打开Windows的cmd,输入C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe exe路径

等待安装。安装完毕之后打开计算机管理,查看服务,点击启动。

***删除服务:sc delete 服务名

 

至此,所有步骤都完成了,可以畅快的调用wcf来发送系统邮件了

>>>>>>>>>>>>>

 

 百度经验 : C#创建Windows服务与安装-图解

 

推荐数据库使用nosql数据库,redis或者mongodb,在接下里的随笔里我会记录mongdb和redis的使用过程。。。第一次发文,紧张死宝宝了

 


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage