Cant send email using VBA

拟墨画扇 提交于 2020-02-06 03:45:24

问题


I am creating a tool that will automatically dispatch emails out.

I have started to build it using very simple code as below:

Set outlookobj = New Outlook.Application
Set mitem = outlookobj.CreateItem(olMailItem)

With mitem
.To = "A_Valid_Email@email.com"
.Subject = "TEST"
.Body = "test"
.Display
.Send

End With
End Sub

However the company I work for seem to have locked down .send. The email will create fine but will not send. Can anyone think of a way around this? I have considered using .sendkeys "^{ENTER}" however I know they are not a good way to doing things.

Thank you in advance Matt


回答1:


I'm referring to this thread: Excel VBA sending mail using Outlook - Send method fails where the solution was the following:

"Change .Send to .Display and put SendKeys "^{ENTER}" before the With mitem line."

You can at least try it, maybe it works for you as well. :)




回答2:


If you're okay with not using Outlook, you can use CDO to send emails. I'm not sure if this will get around your security issue, but it might be worth a try. Here is an example.

Sub CDO_Mail_Small_Text(ByVal RecipientList As String, ByVal From As String, _
        ByVal Subject As String, ByVal Body As String, ByVal Attachment As String, _
        ByVal cc As String)
    Dim iMsg As Message
    Dim iConf As Configuration
    Dim strbody As String
    Dim Flds As Variant

    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1    ' CDO Source Defaults
    Set Flds = iConf.Fields
    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") _
                       = "webmail.zimmer.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With

    With iMsg
        Set .Configuration = iConf
        .To = RecipientList
        .cc = cc
        .From = From
        .Subject = Subject
        .HTMLBody = Body
    End With

    If Attachment <> "" Then
        iMsg.AddAttachment Attachment
    End If

    iMsg.Send

End Sub

By the way, this has the additional advantage of letting you spoof the sender and make it appear as though it came from someone else. I don't know if that's handy or not, but just FYI.



来源:https://stackoverflow.com/questions/34724267/cant-send-email-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!