How to send a mail with attachment automatically?

I'm going to create a couple of files everyday that must be send separately with the filename as subject to an specific email adress.

I'm thinking that I can put the files in one folder and when I want to send them I just start a script or something that creates a mail for every file, attatch the file and send each one of the files in separately mail.

Can someone help me? Can do this in outlook 2007 or if it is possible somehow with Outlook WEBAPP it would be great.

May 8th, 2015 11:28am

Hi,

You can try the following code sample to send all of the files in a folder as email attachments:

Dim fldName As String
Sub SendFilesbuEmail()
' From http://slipstick.me/njpnx
    Dim sFName As String
     
    i = 0
    fldName = "C:\Users\Diane\"
    sFName = Dir(fldName)
    Do While Len(sFName) > 0
      Call SendasAttachment(sFName)
      sFName = Dir
      i = i + 1
      Debug.Print fName
    Loop
    MsgBox i & " files were sent"
 
End Sub
 
Function SendasAttachment(fName As String)
 
Dim olApp As Outlook.Application
Dim olMsg As Outlook.MailItem
Dim olAtt As Outlook.Attachments
 
Set olApp = Outlook.Application
Set olMsg = olApp.CreateItem(0) ' email
Set olAtt = olMsg.Attachments
 
' attach file
olAtt.Add (fldName & fName)
 
' send message
With olMsg
  .Subject = "Here's that file you wanted"
  .To = "alias@domain.com"
  .HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I have attached " & fName & " as you requested."
  .Send
End With
 
End Function

Reference: http://www.slipstick.com/developer/macro-send-files-email/

Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.

Since we are not the best resource for coding, if you need further assistance about coding, I'd recommend you post a question in the following forum:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG

The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

Steve Fan
TechNet Community Support

Free Windows Admin Tool Kit Click here and download it now
May 11th, 2015 2:11am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics