VB.NET操作Lotus收发邮件

使用VS2019,Lotus 9.0.1

添加引用,如下图: 

关于send():

从对象浏览器中,发现只提供:Public Overridable Sub Send(bAttachform As Boolean, Optional ByRef pRecipients As Object = )

从IBM网站(网站不知什么时候会删除相关文档)上查到send method(已经迁移到网站HCL介绍send)介绍如下:

Syntax

public void send()

    throws NotesException
public void send(String recipient)

    throws NotesException
public void send(java.util.Vector recipients)

    throws NotesException
public void send(boolean attachform)

    throws NotesException
public void send(boolean attachform, String recipient)

    throws NotesException
public void send(boolean attachform, java.util.Vector recipients)

    throws NotesException
Note The send() and send(boolean) methods are new with Release 6.

Parameters

String recipient
The recipient of the document. See below.

java.util.Vector recipients
Vector of String elements. The recipients of the document. See below.

boolean attachform
If true, the form is stored and sent along with the document. If false, it isn't. Do not attach a form that uses computed subforms.
'如果为true,则表单将与文档一起存储和发送。 如果是false,则不是。 不要附加使用计算子表单的表单。

无论多简单的邮件,attachform为True时,单击打开收到的邮件都回出现如图警告信息:

另外一个关于send method的说法如下:

Syntax
Call notesDocument.Send( attachForm [, recipients ] )

Parameters

attachForm
Boolean. If True, the form is stored and sent along with the document. If False, it isn't. Do not attach a form that uses computed subforms.

recipients
String or array of strings. Optional. The recipients of the document. See below.

Usage
The following rules apply to specification of the recipient or recipients:
Replaces any recipients in an existing SendTo item.
Required if the document does not contain a SendTo item.
May include people, groups, or mail-in databases.
If you have only Reader access to a database, you can run an agent that creates and sends a document, but the agent will not work if you attach a file to that document.
Two kinds of items affect the mailing of the document when you use Send:
If the document contains additional recipient items, such as CopyTo or BlindCopyTo, Notes mails the documents to these recipients.
If the document contains items to control the routing of mail, such as DeliveryPriority, DeliveryReport, or ReturnReceipt, Notes uses these when sending the document.
The SaveMessageOnSend property controls whether the sent document is saved in the database. If SaveMessageOnSend is true and you attach the form to the document, the form is saved with the document.
Sending the form increases the size of the document, but ensures that the recipient can see all of the items on the document.
The Send method automatically creates an item called $AssistMail on the sent document. The SentByAgent property uses this item to determine if a document was mailed by a script.
If a script runs on a workstation, the mailed document contains the current user name in the From item. If a script is a scheduled agent running on a server, the mailed document contains the agent signer's name in the From item.

 Item.Type属性如下:

Read-only. The data type of an item.
Defined in
Item
Data type
int
Syntax
public int getType()

    throws NotesException
Legal values
Item.ACTIONCD
Item.ASSISTANTINFO
Item.ATTACHMENT (file attachment)
Item.AUTHORS
Item.COLLATION
Item.DATETIMES (date-time or range of date-time values)
Item.EMBEDDEDOBJECT
Item.ERRORITEM (error occurred while getting type)
Item.FORMULA (Domino formula)
Item.HTML (HTML source text)
Item.ICON
Item.LSOBJECT
Item.MIME_PART
Item.NAMES
Item.NOTELINKS (link to a database, view, or document)
Item.NOTEREFS (reference to the parent document)
Item.NUMBERS (number or number list)
Item.OTHEROBJECT
Item.QUERYCD
Item.RICHTEXT
Item.READERS
Item.SIGNATURE
Item.TEXT (text or text list)
Item.UNAVAILABLE
Item.UNKNOWN
Item.USERDATA
Item.USERID
Item.VIEWMAPDATA
Item.VIEWMAPLAYOUT
Usage
You can also test for Names, Readers, and Authors items with:
IsNames
IsReaders
IsAuthors
Language cross-reference
Type property in LotusScript NotesItem class
See examples
Examples: LastModified, Name, Text, Type, and ValueLength properties
Related topics
Accessing item properties in Java classes

我们用得最多的一个:replaceItemValue method介绍如下:

Syntax
public Item replaceItemValue(String name, Object value)

    throws NotesException
Parameters
String name
String. The name of the item or items you want to replace.
Object value
The value of the new item. The data type of the item depends upon the data type of value, and does not need to match the data type of the old item.

Data type of value
Resulting item

String
Text

Integer
Number

Double
Number

DateTime
Date-time item

java.util.Vector with String, Integer, Double, or DateTime elements
Multi-value text, number, or date-time item

Item
Same data type as the Item
Return value
Item
The new item, which replaces all previous items with the same name.

 其实在操作Lotus中,令我比较困惑的一个问题就是,对于replaceItemValue中的第一个参数Item,没有Item.name的更多资料。

用下面代码,找到了我需要的Item如下(绿色的4个):'NotesItem数据类型
        'Item.Name   Item.type.ToString    Item.Text       '说明
        'Form                      Text                 Return Receipt  '回执(退货收据)

        ‘ReturnReceipt        Text                   1                     '是否发送回执
        'IntendedRecipient  Text                                          '预期收件人(回执才有)
        'Principal                 Text                                          '主要(用那一个邮箱发送就显示那个:pFile1.nsf ,就是Function GetDatabase(pServer As String, pFile As String, Optional bCreateonfail As Boolean = True) As Domino.NotesDatabase 的就是第二个参数
        'From                Names、Text                                 '发件人
        'Subject                  Text                                          '主题
        'SendTo            Names、Text                                 '收件人
        'PostedDate                                                            '发送时间

  Dim nv As NotesView = db.GetView("$Sent")   '发件箱 

  Dim doc As NotesDocument = nv.GetLastDocument()     '读取最新邮件 

  Dim item0 As NotesItem 
  Dim i As Integer = 0 

  For Each item0 In doc.Items 
          i += 1 
          WriteRunLog("第 " + Str(i) + " 个") 
          WriteRunLog(item0.type.ToString) 
          WriteRunLog(item0.Name) 
          WriteRunLog(item0.Text) 
  Next 

 最后,写了发送邮件过程如下:

 Private Sub SendEmail3(ByVal Addres As String, ByVal Title As String, ByVal Content As String, ByVal Attachment As String) 

        Try 

            Dim ns As New NotesSession 

            Dim db As NotesDatabase 

            Dim doc As NotesDocument 

            If Not (ns Is Nothing) Then 

                ns.Initialize(MyLoginPara.NotesPwd)     '("password") 

                db = ns.GetDatabase(MyLoginPara.ServerName, MyLoginPara.NotesFileName, False) 

                If Not (db Is Nothing) Then 

                    doc = db.CreateDocument() 

                    doc.ReplaceItemValue("Form", "Memo") 

                    doc.ReplaceItemValue("Subject", Title) 

                    Dim rt As NotesRichTextItem 

                    rt = doc.CreateRichTextItem("Body") 

                    rt.AppendText(Content) 

                    '附件(Attachment为附件文件名)     
                    If Attachment <> "" Then 
                        If File.Exists(Attachment) Then 
                            Dim nrti As NotesRichTextItem = doc.CreateRichTextItem("attachment") 
                            nrti.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT, "", Attachment) 
                            '附件也可以直接添加到Body这个Item中,代码如下。但奇怪的是,将附件直接添加到Body中,图片附件不能以缩略图显示。
                            rt.EmbedObject(EMBED_TYPE.EMBED_ATTACHMENT, "", Attachment)
                        End If 
                    End If 

                    Dim rec_peoples() As String = Addres.Split(",") 
                     
                    doc.ReplaceItemValue("SendTo", rec_peoples)   ‘单个多个收件人都可以发送
                    doc.SaveMessageOnSend = True   '保存到发件箱 
                    doc.ReplaceItemValue("ReturnReceipt", "1")  '发送回执 
                    '当有“禁止拷贝”时,会多下面几项 
                    '$KeepPrivate   "2" 
                    '$NoteHasNativeMIME     "1" 
                    'MIME_Version           "1.0" 
                    'MIMEMailHeaderCharset  "2031619" 
                    '$Orig      "E28F7B39E023E41148258455000ECE8E"  'item.type=NOTEREFS 


                    doc.Send(False) 

                    rt = Nothing 

                    doc = Nothing 
                    'Next 

                End If 

                db = Nothing 

                ns = Nothing 

                GC.Collect() 
            End If 

        Catch ex As Exception 

            'MsgBox(ex.Message.ToString()) 
            WriteRunLog(ex.Message.ToString()) 

        End Try 

    End Sub 

接收邮件:

Private Sub CheckEmail()     
       Dim CheckEmail0 As String 
       Try 
            Dim ns As New NotesSession 

            If ns Is Nothing Then 
                WriteRunLog("No NotesSession") 
                Exit Try 
            End If 

            Dim Gmail As Mail 
            Dim lNow As Date = Now() 
            Dim LastTime As Date  
            Dim k As Integer = DataGridView3.RowCount 

            Dim db As NotesDatabase 
             
            db = ns.GetDatabase(MyLoginPara.ServerName, MyLoginPara.NotesFileName, False) 
            '获取邮箱视图 
            Dim nv As NotesView = db.GetView("$inbox")    '收件箱 
            '遍历所有邮件 
            Dim doc As NotesDocument = nv.GetLastDocument()     '读取最新邮件 

            Dim item0 As NotesItem 
            
            Gmail.From = CType(doc.GetItemValue("From"), Object())(0).ToString      '发件人 
            Gmail.Subject = CType(doc.GetItemValue("Subject"), Object())(0).ToString    '标题 
            Gmail.PostedDate = CType(doc.GetItemValue("PostedDate"), Object())(0).ToString 
            LastTime = Convert.ToDateTime(Gmail.PostedDate) 

            If LastTime > LastCheckEmailTime1 Then 
                Dim i As Integer = 0 

                Do While Convert.ToDateTime(Gmail.PostedDate) > LastCheckEmailTime1 ' And Gmail.PostedDate > lNow.Date.ToString    ' 只查找今天邮件Gmail.PostedDate > lNow.Date.ToString 
                    
                        DataGridView3.RowCount = k + 1 
                        DataGridView3.Rows(k).Cells("发件人").Value = Gmail.From.Split("/")(0).Split("=")(1) 
                        DataGridView3.Rows(k).Cells("主题").Value = Gmail.Subject 
                        DataGridView3.Rows(k).Cells("日期").Value = Convert.ToDateTime(Gmail.PostedDate) 
                        k += 1 
                    doc = nv.GetPrevDocument(doc) 
                    If doc Is Nothing Then 
                        Exit Do 
                    End If 
                    Gmail.From = CType(doc.GetItemValue("From"), Object())(0).ToString      '发件人 

                    Gmail.Subject = CType(doc.GetItemValue("Subject"), Object())(0).ToString    '标题 

                    Gmail.PostedDate = CType(doc.GetItemValue("PostedDate"), Object())(0).ToString 
                    Loop 
                LastCheckEmailTime1 = LastTime 

            Else 
                LastCheckEmailTime1 = lNow 
            End If 
            
            db = Nothing 

            ns = Nothing 

            GC.Collect() 

        Catch ex As Exception 

            MsgBox(ex.Message.ToString()) 
            WriteRunLog(ex.Message.ToString()) 

        End Try 

    End Sub 

下载邮件中的附件:

 Dim o As NotesEmbeddedObject 
 For Each item0 In MyLotus.doc.Items 
            If item0.type = IT_TYPE.RICHTEXT Then 
                If item0.EmbeddedObjects Is Nothing Then 
                    MsgBox("没找到附件") 
                    Continue For 
                End If 

                For Each o In item0.EmbeddedObjects         '因为item0实际为NotesRichTextItem,所以具有属性EmbeddedObjects 
                    MsgBox("找到附件:" & o.Name) 
                    o.ExtractFile("D:\Tmp_D\" & o.Source)   '下载附件到指定目录 
                Next 
            End If 
 Next
 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值