[英]Excel VBA Change From email address with IBM Notes?
I have the following vba code which sends an email from excel using IBM Notes. 我有以下vba代码,可使用IBM Notes从excel发送电子邮件。
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) 专用子Worksheet_Change(按目标的ByVal目标)
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 = "bogus_user@example.com"
nMail.SendTo = "mark.obrien@lidl.co.uk"
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
The NotesDocument.Send method does not allow an authenticated to spoof the sender's From address. NotesDocument.Send方法不允许经过身份验证的欺骗发送者的“发件人”地址。 Code running on the Domino server can do it, but your code is connecting as a client. 在Domino服务器上运行的代码可以做到这一点,但是您的代码正在作为客户端进行连接。
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. 我将提到第一个,但是必须告诉您,IBM不支持它,也不建议您使用它-特别是对于Notes新手开发人员。 It involves writing the document directly to the Domino router mailbox (mail.box) instead of using the NotesDocument.Send method. 它涉及将文档直接写入Domino路由器邮箱(mail.box),而不是使用NotesDocument.Send方法。
The second way is to use code that runs on your Domino server to send the email. 第二种方法是使用在Domino服务器上运行的代码来发送电子邮件。 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. 一种方法是让您的代码将NotesDocument保存在Domino服务器上的数据库中,并在该数据库中有一个后台代理,该代理被设置为在创建新文档时运行。 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. 代理中的代码将设置Principal字段,我看到您已经尝试过-但如上所述,当使用NotesDocument.send在客户端代码中运行时,它不起作用。 There are many other ways. 还有许多其他方法。
As Richard already said, you can not spoof the sender from the client like that unless you use that undocumented method. 正如Richard所说的那样,除非您使用该未记录的方法,否则您不能像这样从客户端欺骗发送者。 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. 我有一个用于邮件通知的Notes类(可以在我的博客上找到),但是Richard是正确的,因为您作为初学者(根据您发布的代码很清楚)可能不应该尝试使用该方法。
On a side note, why do you use such a convoluted way to set the value of TrueRef? 附带说明一下,为什么要使用这种复杂的方法来设置TrueRef的值? Can't you use a Select Case statement? 您不能使用Select Case语句吗? 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.