在Access vba

时间:2017-02-06 22:06:14

标签: vba email access-vba

我想知道是否可以更改Access VBA中的.SenderEmailAddress属性。每当我尝试更改此属性时,我都会收到此错误:

编译错误:

无法指定只读属性

我尝试从我设置的中央通用电子邮件发送电子邮件,以接收客户的反馈。我不希望收到收件人的电子邮件到客户的个人电子邮件/电子邮件。我设置的通用电子邮件包含与我个人相同的域名。

编辑:这不是所列问题的副本。我需要找到一种方法来更改发件人地址,而不仅仅是更改显示的叠加名称。当用户回复时,需要将其发送到通用电子邮件。不是发送它的访问客户端电子邮件。

此外,这是我的代码

Dim myMail      As Outlook.MailItem
Dim myOutlApp   As Outlook.Application

Set myOutlApp = New Outlook.Application
Set myMail = myOutlApp.CreateItem(olMailItem)

With myMail

    ' trying to set the from address
    .SenderEmailAddress = "info@generic.com"

    .To = "customer@othercompany.com"

    .Subject = "Hey"

    .BodyFormat = olFormatHTML

    .HTMLBody = "<!DOCTYPE html>"
    .HTMLBody = .HTMLBody & "<html><head><body>"
    .HTMLBody = .HTMLBody & "<h1><u>This is an example header line</u></h1>"
    .HTMLBody = .HTMLBody & "<h2><u>This is an example header 2 line</u></h2>"
    .HTMLBody = .HTMLBody & "<table>"
    .HTMLBody = .HTMLBody & "<tr><td>Element 1</td><td>" & strElement1 & "</td></tr>"
    .HTMLBody = .HTMLBody & "<tr><td>Element 2</td><td>" & strElement2 & "</td></tr>"
    .HTMLBody = .HTMLBody & "</table>"
    .HTMLBody = .HTMLBody & "</body></html>"

    .Display

End With

Set myMail = Nothing
Set myOutlApp = Nothing`

1 个答案:

答案 0 :(得分:1)

使用CDO对象电子邮件而不是mapi对象,下面的示例代码,从link

复制
Public Sub SendEmail()
  Const cdoSendUsingPickup = 1
  Const cdoSendUsingPort = 2
  Const cdoAnonymous = 0
  Const cdoBasic = 1     ' Use basic (clear-text) authentication
  Const cdoNTLM = 2  'NTLM  ' Use NTLM authentication
  Dim imsg As Object
  Dim iconf As Object
  Dim flds As Object
  Dim schema As String

  Set imsg = CreateObject("CDO.Message")
  Set iconf = CreateObject("CDO.Configuration")
  Set flds = iconf.Fields

  ' send one copy with SMTP server (with autentication)
  schema = "http://schemas.microsoft.com/cdo/configuration/"
  flds.Item(schema & "sendusing") = cdoSendUsingPort
  flds.Item(schema & "smtpserver") = "smtp.mail.yahoo.com"
  flds.Item(schema & "smtpserverport") = 465
  flds.Item(schema & "smtpauthenticate") = cdoBasic
  flds.Item(schema & "sendusername") = "scammera1@yahoo.com.hk"
  flds.Item(schema & "sendpassword") = "password"
  flds.Item(schema & "smtpusessl") = True
  flds.Update

 With imsg
    .From = "scammera1@yahoo.com.hk"   'Should be same as sendusername
    .To = "scammera1@yahoo.com.hk,terethan@hotmail.com"
    '.Bcc = "test@email.com"
    '.Cc = "test@email.com"
    .Subject = "This is a subject"
    .HTMLBody = "<h1>This is a test message<h1>"        
    'TextBody = "This is a test message"         
    .Sender = "This is the sender"       
    '.Organization = "My Company"        
    '.ReplyTo = "address@mycompany.com"        
    '.AddAttachment "c:\temp\readme.txt"                
     .Send     
   End With
 Set .Configuration = iconf  
 Set iconf = Nothing     
 Set imsg = Nothing     
 Set flds = Nothing 
End Sub