Dim oEMail As New CDONTS.EMail
oEMail.From "anyname@anydomain.com"
oEMail.To "youremail@yourdomain.com"
oEMail.BodyFormat = CdoBodyFormatText
oEMail.Body = "Insert some useful text here"
oEMail.Importance = CdoHigh
oEMail.AttachFile "C:\filename.txt"
oEMail.Send
How do you introduce a "from" field
I do not believe that Outlook will allow you to automate a FROM since Outlook tries to prevent spoofing. If I am wrong, I would love to see the code on how to do it. ;-)
Set outRecipient = outNameSpace.CreateRecipient("emailaddress")
Set outFolder = outNameSpace.GetSharedDefaultFolder(outRecipient, olFolderInbox)
Set outItem = outFolder.Items.Add("IPM.NOTE")
Public Function SetupOutlookEmail(ByVal sTo As String, ByVal sSubject As String, ByVal sBody As String, ParamArray sAttachmentList() As Variant) As Boolean
On Error GoTo Err_SetupOutlookEmail
Dim objOLApp As Object
Dim outItem As Object
Dim outFolder As Object
Dim DestFolder As Object
Dim outNameSpace As Object
Dim lngAttachment As Long
Set objOLApp = CreateObject("Outlook.Application")
Set outNameSpace = objOLApp.GetNamespace("MAPI")
Set outFolder = outNameSpace.GetDefaultFolder(6)
Set outItem = objOLApp.CreateItem(0)
Set outRecipient = outNameSpace.CreateRecipient("[COLOR=Blue][I]myemail[/I][/COLOR]")
Set outFolder = outNameSpace.GetSharedDefaultFolder(outRecipient, olFolderInbox)
Set outItem = outFolder.Items.Add("IPM.NOTE")
outItem.To = sTo
outItem.Subject = sSubject
outItem.HTMLBody = sBody
outItem.ReadReceiptRequested = False
You can't do that using the Outlook object model. You would have to use
Extended MAPI (C++ or Delphi code only), CDO 1.21 (optional installation
with Outlook 2000 and later and subject to the security prompts) or
Redemption (3rd party library at www.dimastr.com/redemption) to do that.
See the code example for sending with a different From address at
www.cdolive.com/cdo5.htm for a CDO 1.21 way of doing it.
Ok I was wrong you cannot do it with the Outlook object model. Seems a pretty simple thing to me for M$ to implement but I sometimes feel they are getting more of an expert about making excuse than doing some real work.
In case the below link fails;
http://www.pcreview.co.uk/forums/showpost.php?p=5448309&postcount=3
I do not believe that Outlook will allow you to automate a FROM since Outlook tries to prevent spoofing. If I am wrong, I would love to see the code on how to do it. ;-)
It did hurt me to write "I was wrong" but I was thinking of you and my previous comments.Which is why i previously stated in this thread...
Public Sub CreateEmail(strFrom As String, strTo As String, strCC As String,
strBCC As String, strSubject As String, strTextOrHTML As String, strBodyText
As String, strBodyHTML As String)
On Error GoTo Err_CreateEmail
Dim OlApp As Outlook.Application
Dim olAccounts As Outlook.Accounts
Dim olAccount As Outlook.Account
Dim olAccountTemp As Outlook.Account
Dim olMail As MailItem
Dim FoundAccount As Boolean
Set OlApp = New Outlook.Application
FoundAccount = False
'loop through and find Outlook account based on from email address
Set olAccounts = OlApp.Application.Session.Accounts
For Each olAccountTemp In olAccounts
If (olAccountTemp.smtpAddress = strFrom) Then
Set olAccount = olAccountTemp
FoundAccount = True
Exit For
End If
Next
If (FoundAccount) Then
Set olMail = OlApp.CreateItem(olMailItem)
With olMail
.SendUsingAccount = olAccount
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = strSubject
If (strTextOrHTML = "HTML") Then
.BodyFormat = olFormatHTML
.Body = strBodyText
.HTMLBody = strBodyHTML
Else
.BodyFormat = olFormatPlain
.Body = strBodyText
End If
' .Attachments.Add filename
.Display
End With
Else
MsgBox "Could not find the appropriate Outlook account for email
address: " & strFrom & ". Contact RPT Software for assistance if needed.",
vbOKOnly, "Outlook error retrieving SMTP account"
End If
Exit_CreateEmail:
Set olMail = Nothing
Set olAccount = Nothing
Set OlApp = Nothing
Exit Sub
Err_CreateEmail:
MsgBox Err.Description
Resume Exit_CreateEmail
End Sub