HI. I greatly appreciate any help or advice in advance. I am currently using the following ms access which sends an email to a specified outlook account. The code also has other functions, but my main concern is adding something to the email objects which will let me specify an account the email is coming from. As it is now, it only shows the sender as the default account. However, it should be sent from a generic account.
Private Sub cmdSendEmail_Click()
Dim strEmailName As String
Dim strEmailAddress As String
Dim strCurrentAttempt As String
Dim sRecipient As String
Dim iPreviousAttempts As Integer
Dim objProp As UserProperty
Dim strBody As String
Dim objOutlook As Outlook.Application
Dim objNameSpace As NameSpace
Dim objFolder As MAPIFolder
Dim objEmail As Outlook.MailItem
Dim rec As Recordset
Dim HistoryRec As Recordset
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Dim BodyFile As String
' start by createthe FileSystemObject
Set fso = New FileSystemObject
' creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objNameSpace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objEmail = objOutlook.CreateItem(olMailItem)
' Setup our connection to the database
Set rec = Me.Recordset
' create string with email address
If (IsNull(rec("Email_ID")) = True) Then
MsgBox "E-Mail/Core_ID is not filled in", vbOKOnly
GoTo EndOfSub
Else
strEmailAddress = rec("email_id") & "@company.com"
End If
If (IsNull(rec("username")) = True) Then
strEmailName = ""
Else
strEmailName = rec("username")
End If
If (IsNull(rec("Attempt")) = True) Then
strCurrentAttempt = ""
Else
strCurrentAttempt = Trim(rec("Attempt"))
End If
' determine the email attempt and fill the body
' with the correct message
If (strCurrentAttempt = "") Then
' Setup our FIRST Attempt message
iPreviousAttempts = 0
BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile1.txt"
' open the file for reading
Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile1.txt", ForReading, False, TristateUseDefault)
' dump the contents of the file into the MyBodyText string
MyBodyText = MyBody.ReadAll
' close the file
MyBody.Close
ElseIf (Left(strCurrentAttempt, 12) = "1st Attempt ") Then
' Setup our SECOND Attempt message
iPreviousAttempts = 1
BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile2.txt"
Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile2.txt", ForReading, False, TristateUseDefault)
MyBodyText = MyBody.ReadAll
MyBody.Close
ElseIf (Left(strCurrentAttempt, 12) = "2nd Attempt ") Then
' Setup our THIRD/LAST Attempt message
iPreviousAttempts = 2
BodyFile$ = "U:\Data_Files\Flat_Files_Email\flatfile3.txt"
Set MyBody = fso.OpenTextFile("U:\Data_Files\Flat_Files_Email\f latfile3.txt", ForReading, False, TristateUseDefault)
MyBodyText = MyBody.ReadAll
MyBody.Close
Else
' More than three attempts and we just ignore the email
MsgBox "Three attempts have been made", vbOKOnly
GoTo EndOfSub
End If
If (BodyFile = "") Then
MsgBox "Body is empty", vbOKOnly
GoTo EndOfSub
End If
'creates and sends email
With objEmail
.To = sRecipient
.Subject = "Your request for your next computer."
.Body = MyBodyText
.Send
End With
EndOfSub:
'Closes outlook. remove you do not want to close outlook
' objOutlook.Quit
Set objOutlook = Nothing
Set objNameSpace = Nothing
Set objFolder = Nothing
Set objEmail = Nothing
' Cause the subform to refresh
Refresh
End Sub
Please let me know if you have any suggestions.