E-mail message

farmock

Board Regular
Joined
Sep 10, 2006
Messages
60
I am using Joe Was's method to send an email message as below:

Sub Mailer()
'Mails without security alert

'Need reference to Outlook in the Project References

Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Set objmail = objol.CreateItem(olMailItem)
Dim strbody As String
Dim pathname As String

strbody = "Enter Subject Body Here"
pathname = "Pathname of Attachment"

With objmail
'Email To = Email Address in quotes
.To = "Email Address Here"
.CC = "Email Address Here"

.Subject = "Subject" '
'Message Body
.Body = "Message Body" & _
vbCrLf & vbCrLf & strbody & _
vbCrLf & vbCrLf & "Kind Regards" & vbCrLf & _
vbCrLf & "Your Name" & vbCrLf & "Job Title"
'Does not Expire
.NoAging = True
'Read Receipt
.ReadReceiptRequested = True

'adds attachment to email
.Attachments.Add pathname

'Display Email
.Display
End With
Set objmail = Nothing
Set objol = Nothing

'Used for Sending Email Automatically
Application.Wait (Now + TimeValue("0:00:02"))
Application.SendKeys "%s"
Application.Wait (Now + TimeValue("0:00:02"))

SendKeys "%{s}", True 'send the email without prompts

End Sub

It works fine but my message ends up in the outbox in Outlook and does not get sent until I open Outlook. Is there a way to send it directly or is there a way I can open and close Outlook with code so the message will be sent. Thanks for any help you can provide. Frank
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

farmock

Board Regular
Joined
Sep 10, 2006
Messages
60
Hatman,
Thanks for the response. I'm new at this and am not sure where the attachmnet file names get entered. Thanks for all the help! Frank
 

hatman

Well-known Member
Joined
Apr 8, 2005
Messages
2,664
Check out the Sample_Call routine at the end of the code... it shows how to attach 2 files.

Alternatively, to attach a single file, simply assign a single value to the variable.

Even more simply:
Code:
    Send_New_Mail "paul.sasur@hs.utc.com", "SUBJECT", "Body Text Here", "c:\temp\sample file.txt"
 

farmock

Board Regular
Joined
Sep 10, 2006
Messages
60

ADVERTISEMENT

Hatman,
I now understand. Thanks again for the help. Frank
 

farmock

Board Regular
Joined
Sep 10, 2006
Messages
60
Hatman,
When I run the program when it gets to the line near the end "If OlbCreated Then OutApp.Quit" I get a message asking if I want to save "subject". If I save it, it shows up in Outlook in the inbox with the message "This mail has not been sent". If I don't save it, it doesn't show up at all and doesn't get sent. Any thoughts on what I might be doing wrong? Thanks for the help. Frank
 

Forum statistics

Threads
1,136,263
Messages
5,674,710
Members
419,521
Latest member
Jasonnie

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
Top