简体   繁体   中英

Sending IBM Lotus Notes email using excel VBA sends from wrong email address

I currently have two Lotus Notes databases, each with their own email addresses associated with them. As expected, when I send an email through the first database to my gmail account, it shows up as "From: notesAccount1@db1.com" and when I send using the second database, the message shows up as "From: notesAccount2@db2.com" in my gmail.

I have working code that opens up the second database , searches the inbox for an email containing a certain keyword, and extracts an email address from that email. It then creates a new document in that second database , inserts the recipients name, subject, body, etc., sends the email, and saves it to my sent folder. Everything works smoothly up to this point.

However, when I send an email from the second database to my gmail account using this VBA method, the email shows up in my gmail as "From: notesAccount1@db1.com" - the email associated with the first database.

I can't figure out why this is happening. Pretty limited knowledge of the interactions between VBA and Lotus Notes, and Lotus Notes servers/databases in general. The first database is technically my default one that only I have access to and the second database was added later and multiple people have access to it. I don't know if that's relevant.

Would appreciate any help! Thanks.

Note: This code was copied and adapted from several sources, including some on SO, IBM and other Notes sources, and anything else Google threw my way, including http://www.fabalou.com/vbandvba/lotusnotesmail.asp

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

Code: (This will have to be adapted as I have taken out server names and mail file names)

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 = testName@test.com

        '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

Notes will normally send the mail using the email address for the ID you use to login. So if you login using notesAccount1/Domain, then all emails will be coming from notesAccount1@example.com. If you want to fake the sender, you need to use an undocumented method: inject the email directly into mail.box. You should not attempt to do this unless you really know what you are doing.

I have posted code on my blog for a mail notification class, it supports this work-around to set the sender on outgoing emails. You can find the latest code at http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM