0
votes

I have the following vba code which sends an email from excel using IBM Notes.

However, i want to be able to change the from address. Pleas can someone show me where i am going wrong?

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("M:M")) Is Nothing Then
    If Target.Cells.Count < 3 Then


  'Set up the objects required for Automation into lotus notes

    Dim Ref As String
    Dim TrueRef As String



    Ref = Range("H" & (ActiveCell.Row)).Value

    If Ref = "WSM" Then
    TrueRef = "WES"
    Else
    If Ref = "NAY" Then
    TrueRef = "NAY"
    Else
    If Ref = "ENF" Then
    TrueRef = "ENF"
    Else
    If Ref = "LUT" Then
    TrueRef = "MAG"
    Else
    If Ref = "NFL" Then
    TrueRef = "NOR"
    Else
    If Ref = "RUN" Then
    TrueRef = "RUN"
    Else
    If Ref = "SOU" Then
    TrueRef = "SOU"
    Else
    If Ref = "SOU" Then
    TrueRef = "SOU"
    Else
    If Ref = "BRI" Then
    TrueRef = "BRI"
    Else
    If Ref = "LIV" Then
    TrueRef = "LIV"
    Else
    If Ref = "BEL" Then
    TrueRef = "BEL"
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If
    End If




    ''''''''''''''''''''''''''''''''''

    Dim nMailBody As String
    Dim nMailSubject As String
    Dim nMailRecipient As Variant
    Dim nMail As Object
    Dim nSession As Object
    Dim nDatabase As Object
    Dim nMime As Object
    Dim nMailStream As Object
    Dim nChild As Object
    Dim nSomeMailBodyText As String
    Dim amountOfRecipients As Integer

    nSomeMailBodyText = "<p>Hello,</p><br><p>How are you?</p>"


    nMailSubject = "A great email"

    Set nSession = CreateObject("Notes.NotesSession")
    Set nDatabase = nSession.GETDATABASE("", "")
    Call nDatabase.OPENMAIL
    Set nMail = nDatabase.CREATEDOCUMENT




    nMail.Principal = "[email protected]"

    nMail.SendTo = "[email protected]"
    nMail.subject = "This is test"

    nSession.CONVERTMIME = False
    Set nMime = nMail.CREATEMIMEENTITY
    Set nMailStream = nSession.CREATESTREAM


    'vBody contient le texte au format Html
    Call nMailStream.WRITETEXT(nSomeMailBodyText)
    Call nMailStream.WRITETEXT(" - and again - ")
    Call nMailStream.WRITETEXT(nSomeMailBodyText)
    Call nMailStream.WRITETEXT("<br>")
    Call nMailStream.WRITETEXT("<br>")



    '----- READ AND PASTE SIGNATURE -------------------------------------

    'Get the standard signature location
    nSignatureLocation = nDatabase.GETPROFILEDOCUMENT("CalendarProfile").GETITEMVALUE("Signature")(0)



    Set nChild = nMime.CREATECHILDENTITY
    Call nChild.SETCONTENTFROMTEXT(nMailStream, "text/html;charset=iso-8859-1", ENC_NONE)
    Call nMailStream.Close
    nSession.CONVERTMIME = True



    'Send the document
    nMail.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    nMail.SEND 0, Recipient




    End If
End If

End Sub
2

2 Answers

0
votes

As Richard already said, you can not spoof the sender from the client like that unless you use that undocumented method. I have a Notes class for mail notifications (can be found on my blog), but Richard is correct in that you as a beginner (which is pretty clear based on the code you posted) probably not should attempt using that method.

On a side note, why do you use such a convoluted way to set the value of TrueRef? Can't you use a Select Case statement? Or even just simplify your code:

TrueRef = Ref
If Ref = "WSM" Then
    TrueRef = "WES"
ElseIf Ref = "LUT" Then
    TrueRef = "MAG"
ElseIf Ref = "NFL" Then
    TrueRef = "NOR"
End If

or

If Ref = "WSM" Then
    TrueRef = "WES"
ElseIf Ref = "LUT" Then
    TrueRef = "MAG"
ElseIf Ref = "NFL" Then
    TrueRef = "NOR"
Else
    TrueRef = Ref
End If
0
votes

The NotesDocument.Send method does not allow an authenticated to spoof the sender's From address. Code running on the Domino server can do it, but your code is connecting as a client.

Thre are two ways around this. I will mention the first one, but must tell you that it is not supported by IBM and is not recommended - especially for novice Notes developers. It involves writing the document directly to the Domino router mailbox (mail.box) instead of using the NotesDocument.Send method.

The second way is to use code that runs on your Domino server to send the email. One way to do this would be to have your code save the NotesDocument in a database on the Domino server and have a background agent in that database that is set up to run whenever new documents are created. The code in the agent would set the Principal field, which I see you have tried - but as I said above, it doesn't work when running in client-side code using NotesDocument.send. There are many other ways.