2017-07-07 422 views
1

我目前有两个Lotus Notes数据库,每个数据库都有自己的电子邮件地址与它们相关联。正如所料,当我通过第一个数据库发送邮件到我的Gmail账户时,它显示为“From:[email protected]”,当我使用第二个数据库发送时,邮件显示为“From:notesAccount2 @ db2 .com“在我的Gmail中。使用excel发送IBM Lotus Notes电子邮件VBA从错误的电子邮件地址发送

我有工作代码打开第二个数据库,在收件箱中搜索包含某个关键字的电子邮件,并从该电子邮件中提取电子邮件地址。然后在第二个数据库中创建一个新文档,插入收件人姓名,主题,正文等,发送电子邮件并将其保存到我发送的文件夹中。到目前为止,一切都很顺利。

但是,当我使用此VBA方法从第二个数据库向我的gmail帐户发送电子邮件时,该电子邮件在我的Gmail中显示为“From:[email protected]” - 与第一个数据库关联的电子邮件。

我不明白为什么会发生这种情况。 VBA和Lotus Notes以及一般的Lotus Notes服务器/数据库之间的交互知识相当有限。 第一个数据库在技术上是我默认的数据库,只有我有权访问并且第二个数据库稍后添加并且有多个人可以访问它。我不知道这是否相关。

将不胜感激任何帮助!谢谢。

注:此代码被复制和改编自几个来源,包括一些对SO,IBM和其他注意事项的来源,以及其他任何谷歌把我的方式,包括 http://www.fabalou.com/vbandvba/lotusnotesmail.asp

http://www-01.ibm.com/support/docview.wss?uid=swg21178583

代码:(这将不得不适应,因为我已经取出服务器名称和邮件文件名)

Sub ReadNotesEmail() 

Dim sess As Object 
Dim db As Object 
Dim folder As Object 
Dim docNext As Object 
Dim memoSenders As Variant 
Dim newEmail As Object 
Dim view As Object 
Dim entry As Object 
Dim entries As Object 
Dim templateEmail As Object 

Dim mailServer As String 
Dim mailFile As String 
Dim folderName As String 
Dim todayDate As String 
Dim memoBody As String 
Dim senderEmail As String 

Dim emailStartPos As Integer 
Dim emailEndPos As Integer 

'This program will search a particular folder in a Notes database that I designate. 
'It will search that folder for emails that contain certain key words. Once it finds 
'an email that fits, it will grab the sender's email address (written in the body, not 
'in the 'from') and send them an email. 

'Name of folder to search for emails 
folderName = "($Inbox)" 

'Create a Lotus Notes session and open it (will require password) 
Set sess = CreateObject("Lotus.NotesSession") 
sess.Initialize ("") 

'Set the mail server, mail file, and database. This will be the tricky part as I need this to 
'look at the second mail server as opposed to the default mail server of jdyagoda 
Set db = sess.GETDATABASE("***name of second Notes server***", "***name of second mail file***") 

'Open the mail database in notes 
If Not db.IsOpen = True Then 
    Call db.Open 
End If 
Set folder = db.GetView(folderName) 

'Now look through the emails one at a time with a loop that ends when no emails are left. 
'If an email contains the key word, look for the email address of the person who submitted 
'the contact-us form. It follows the string "Email:" and preceeds 
'the string "Phone:". 
Set doc = folder.GetFirstDocument 
Do Until doc Is Nothing 
    Set docNext = folder.GETNEXTDOCUMENT(doc) 
    memoBody = LCase(doc.GetItemValue("body")(0)) 
    If (memoBody Like "*") Then 'This is where you designate the keyword 

     'Here's where you extract the email address - taken out for the purpose of this SO question 
     'senderEmail = [email protected] 

     'Now create a new email to the intended recipient 

     Set newEmail = db.CREATEDOCUMENT 
     Call newEmail.ReplaceItemValue("Form", "Memo") 
     Call newEmail.ReplaceItemValue("SendTo", senderEmail) 
     Call newEmail.ReplaceItemValue("Subject", "Thank you for your email") 
     Call newEmail.ReplaceItemValue("body", "Test Body 1. This is a test.") 
     newEmail.SAVEMESSAGEONSEND = True 

     'Send the new email 

     Call newEmail.ReplaceItemValue("PostedDate", Now()) 'Gets the mail to appeaer in the sent items folder 
     Call newEmail.SEND(False) 

    End If 
    Set doc = docNext 
Loop 

End Sub 

回答

2

备注通常会使用您用于登录的ID的电子邮件地址发送邮件。因此,如果您使用notesAccount1/Domain登录,则所有电子邮件将来自[email protected]。 如果您想伪造发件人,则需要使用未记录的方法:将电子邮件直接注入mail.box。 除非你真的知道你在做什么,否则你不应该试图去做这件事。

我在我的博客上发布了一个邮件通知类的代码,它支持这种解决方法来设置发件人在外发电子邮件。你可以在http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/

+0

找到最新的代码谢谢你的回应,卡尔。这个问题当然是有道理的。不幸的是,IT人员告诉我,没有办法让老一点的人直接登录到第二个帐户。我很欣赏你所关联的潜在解决方法(顺便说一句,写得非常好),但是我认为这对我正在尝试做的事来说有点过头了。我想知道它是如何工作的,例如,如果我点击“新建”手动(而不是通过VBA)从第二个帐户创建并发送一封新邮件,那么它如何将第二个帐户列为发件人?有没有办法通过VBA使用默认类复制它? – jdyagoda

+0

例如,不是通过程序发送每封电子邮件,我可以将该电子邮件保存在草稿文件夹中,然后进入并手动发送所有草稿放在那里与代码?由于我是从第二个帐户手动发送的,发件人的电子邮件是否显示为第二个? – jdyagoda

+0

您所指的是Notes客户端的一项功能,它可以在首选项当前打开的邮件数据库中找到Owner字段,并用它来发送邮件。这种神奇的作用正是因为Notes客户端完成了Karl-Henry所描述的:它直接将消息写入mail.box文件。该功能不能通过VBA中使用的Notes COM类中的Send()方法提供。这是班级开发人员故意选择冒充发件人(有意或无意)难以做到的事情。 –

相关问题