VBA SMTP Sending Outlook mail has no errors, but nothing being sent.

countryfan_nt

Well-known Member
Joined
May 19, 2004
Messages
765
Hello friends, Hope all is well!

I am confused, there is nothing wrong with the code below (at least to my humble knowledge).
when the code reaches the line .Send, it pauses for say 15-20 seconds, and the code resumes, but nothing sent on outlook.

I used the same code in my previous organization and it was working. and there are no bugs.

Your kind help please!
Thank you for your continuous support

VBA Code:
Sub Send_Email(ByVal mailTo As String, _
                ByVal mailCC As String, _
                ByVal mailBCC As String, _
                ByVal mailSubject As String, _
                ByVal mailBody As String, _
                ByVal mailAttachment As String)
                      
    Dim CDO_Mail_Object As Object
    Dim CDO_Config As Object
    Dim SMTP_Config As Variant
    
    On Error GoTo debugs
    
    Set CDO_Mail_Object = CreateObject("CDO.Message")
    Set CDO_Config = CreateObject("CDO.Configuration")
    CDO_Config.Load -1
    Set SMTP_Config = CDO_Config.Fields
    
    With SMTP_Config
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'please put your server name below
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "health.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With
    
    With CDO_Mail_Object
        Set .Configuration = CDO_Config
        
        .From = GetUserID & "@health.com"
        .To = mailTo
        .CC = mailCC
        .BCC = mailBCC
        .Subject = mailSubject
        .TextBody = mailBody
        If mailAttachment <> "" Then .AddAttachment mailAttachment
        .Send
    End With
    
Exit Sub
debugs:
    'If Err.Description <> "" Then MsgBox Err.Description
End Sub
 
Just to elaborate, my version of the code asks Outlook to send the mail. Do you have Outlook installed as a desktop application?

If so, Outlook does whatever it does when you use it to send an email manually, so there is nothing that would be disabled. If VBA is allowed to run in your system at all, then we should be able to get this to work.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Just to elaborate, my version of the code asks Outlook to send the mail. Do you have Outlook installed as a desktop application?

If so, Outlook does whatever it does when you use it to send an email manually, so there is nothing that would be disabled. If VBA is allowed to run in your system at all, then we should be able to get this to work.
Do you mind if I send the workbook electronically to you? perhaps I am doing something elsewhere
 
Upvote 0
I added the following code module Jazzer to your file and mailed it back to you.

My test to run your original code ran without error but did not actually send any mail. I am not familiar with the CDO method, in particular setting the attributes of Item, so I'm not sure what the problem is. Your code looks consistent with other examples I found but I'm not sure what's wrong and I don't know to troubleshoot it without a lot more digging.

The code includes my code for sending through Outlook, which I already posted but I'll show it here again. This test ran perfectly for me. I suggest you run sub SendEMailOutlook_TEST and tell me what happens.

The references to domain.com are fake. I used my own personal domain name, which I prefer not to expose here.
VBA Code:
Option Explicit


' Item types
Public Const olAppointmentItem = 1
Public Const olContactItem = 2
Public Const olDistributionListItem = 7
Public Const olJournalItem = 4
Public Const olMailItem = 0
Public Const olNoteItem = 5
Public Const olPostItem = 6
Public Const olTaskItem = 3

Public Const olMeeting = 1

Public Const olRequired = 1

Public Const olResource = 3

Public Const olTo = 1
Public Const olCC = 2
Public Const olBCC = 3

Public Const olimportancenormal = 1

Public Const olbyvalue = 1


' Classes
Public Const olMail = 43
Public Const olMeetingRequest = 53

' Late Binding, no references required
' Use recipient Subs to add To and cc recipients
Public Sub SendEMailOutlook(MailTo As String, mailcc As String, MailBCC As String, Subject As String, Body As String, Optional AttachmentPath As String = "", Optional DisplayOnly As Boolean = True)


   Dim olMail As Object ' MailItem
   Dim OutlookApp As Object
   Dim ToRecipient As Object ' Recipient
   Dim CCRecipient As Object ' Recipient
   Dim BCCRecipient As Object ' Recipient
  
   Set OutlookApp = GetObject(, "Outlook.Application")
   
   Set olMail = OutlookApp.CreateItem(olMailItem)
   
   With olMail
       .Subject = Subject
       .Importance = olimportancenormal
       If AttachmentPath <> "" Then
         .Attachments.Add AttachmentPath, olbyvalue
       End If
       .Body = Body
   End With
     
   Set CCRecipient = olMail.Recipients.Add(mailcc)
  
   CCRecipient.Type = olCC
   CCRecipient.Resolve
  
  
   Set ToRecipient = olMail.Recipients.Add(MailTo)
  
   ToRecipient.Type = olTo
   ToRecipient.Resolve

   Set BCCRecipient = olMail.Recipients.Add(MailBCC)
  
   BCCRecipient.Type = olBCC
   BCCRecipient.Resolve
  
   If DisplayOnly Then
      olMail.display
   Else
      olMail.Send
   End If
   
End Sub

Public Sub SendEMailOutlook_TEST()

   SendEMailOutlook MailTo:="mailtest@domain.com", _
                    mailcc:="mailtestcc@domain.com", _
                    MailBCC:="mailtestbcc@domain.com", _
                    Subject:="test subject", _
                    Body:="test body"

   MsgBox "Outlook test complete"
  
End Sub

Public Sub SendEmailCDO_TEST()

   MIS_Tools.Send_Email MailTo:="mailtest@domain.com", _
                    mailcc:="mailtestcc@domain.com", _
                    MailBCC:="mailtestbcc@domain.com", _
                    mailSubject:="test subject", _
                    mailBody:="test body", _
                    mailAttachment:=""
                   
   MsgBox "CDO test complete"

End Sub
 
Upvote 0
The smtpserver name looks odd to me. in the original post. If sticking with cdo did you try my suggestion to get your values from an email that you sent to yourself?
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top