Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to send a mail through macro?

Hi All,

I want to know How to send a mail through macro?

I have write the script like below , but it is not working

function sendMailTest()

          Dim objEmail

          Const cdoSendUsingPort = 2           ' Send the message using SMTP

          Const cdoAnonymous = 0           'Do not authenticate

          Const cdoBasic = 1                     'basic (clear-text) authentication

          Const cdoNTLM = 2                     'NTLM

          Const SMTPServer = "mail.myserver.net"

          Const SMTPPort = 25                             ' Port number for SMTP

          Const SMTPTimeout = 60                          ' Timeout for SMTP in seconds

// Till here Script is working

// From here the script is not working

          'Sending mail

          Set objEmail = CreateObject("CDO.Message")

          Set objConf = objEmail.Configuration

          Set objFlds = objConf.Fields

          With objFlds

                    '---------------------------------------------------------------------

                       ' SMTP server details

                       .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort

                       .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer

                       .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous

                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort

                    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

                      .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = SMTPTimeout

                       .Update

                    '---------------------------------------------------------------------

          End With

          objEmail.To = "maneeswaran.kannan@Myserver.com"

          objEmail.From = "kabilan.kumarasamy@Myserver.com"

          objEmail.Subject = "test"

          objEmail.TextBody = "message de test"

          objEmail.Send

          Set objFlds = Nothing

          Set objConf = Nothing

          Set objEmail = Nothing

          msgbox ("Test Mail Sent")

end function

Please help me on this issue..

Thanks In advance...

Regards,

Kabilan K

2 Replies
Not applicable
Author

Try this:

sub Email


Set myApp = CreateObject ("Outlook.Application")

Set myMessage = myApp.CreateItem(olMailItem)

myMessage.BodyFormat = 3 'Outlook.OlBodyFormat.olFormatRichText



myMessage.To = InputBox("Enter email address in johnsmith@mail.com format","Email Address")

myMessage.Subject = "Test"

myMessage.Body = "Test Email.................................."



myMessage.Send

MsgBox("Test Email Sent")



Set myMessage = Nothing

Set myApp = Nothing

Set myInspector = Nothing

Set myDoc = Nothing



end sub



Not applicable
Author

Hi,

In your code also not working till this script Set myApp = CreateObject ("Outlook.Application")

Regards,

Kabilan K.