On Wed, 30 Mar 2016, at 12:43 PM, Ted Roche wrote:
On Wed, Mar 30, 2016 at 7:02 AM, Laurie Alvey trukker41@gmail.com wrote:
I currently use CDO to send email from my apps but I'm thinking of using a .NET component to do it.
May I ask, "Why?"
Well, CDO is tied to Exchange Server and to my mind is gradually being deprecated.
To answer Laurie's question, I had a similar requirement recently and I achieved it by writing a .NET solution that uses the System.Net.Mail namespace and the SMTP functionality therein. It basically creates a fairly 'flat' interface, i.e.
SmtpEmailer.AddAttachment(string) SmtpEmailer.AddLinkedImage(string) SmtpEmailer.AddRecipient(string, string, string) SmtpEmailer.BuildImageTag(string, string) SmtpEmailer.Dispose() SmtpEmailer.Dispose(bool) SmtpEmailer.SendMessage() SmtpEmailer.SetBody(string) SmtpEmailer.SetBodyFormat(string) SmtpEmailer.SetCredentials(string, string, string) SmtpEmailer.SetFrom(string, string) SmtpEmailer.SetPriority(string) SmtpEmailer.SetSubject(string) SmtpEmailer.SmtpEmailer() SmtpEmailer.StartMessage() SmtpEmailer.BodyText SmtpEmailer.EnableSSL SmtpEmailer.ExceptionMessage SmtpEmailer.LastException SmtpEmailer.LastStatusCode SmtpEmailer.Port SmtpEmailer.Server SmtpEmailer.StatusCode SmtpEmailer.TimeoutSeconds SmtpEmailer.Version
This handles HTML mail with inline images (and automatically generates a text version too). Also SSL.
So that spits out a DLL, how do I use that from VFP? You could create a COM wrapper for it, but that woudl then involve building an installer and everything that entails.
What I do is use Rick Strahl's wwdotnetbridge class which allows the use of .NET assemblies directly from VFP with no installation (other than .NET 4.0 or later being present). This means 'xcopy' deployment and no messing with COM. Given the .NET DLL above and the relevant files for wwdotnetbridge in the same folder, you can just use this to send an email from VFP:
DO wwDotNetBridge LOCAL loBridge as wwDotNetBridge
loBridge = GetwwDotnetBridge() loBridge.LoadAssembly("my_dotnet_smtp.dll") && -- The .NET DLL
Local loW as my_dotnet_smtp.SmtpEmailer loW = loBridge.CreateInstance("my_dotnet_smtp.SmtpEmailer") loW.EnableSSL = .t. loW.Port = 587 loW.Server = "my.smtpserver.com" loW.SetCredentials("username", "password", "") loW.StartMessage() loW.AddRecipient("joe.bloggs@gmail.com", "Alan", "") loW.SetSubject("Here's an email.") loW.SetBodyFormat("HTML") lcLogoCID = loW.AddLinkedImage("c:\temp\logo.jpg") lcMyHTML = "<p>Title text</p>" + loW.BuildImageTag(lcLogoCID, "logo") loW.SetBody(lcMyHTML) loW.SetPriority("HIGH") loW.SetFrom("youremail@yourcompany.com", "Your Name")
try if !loW.SendMessage() ? loW.ExceptionMessage ? loW.StatusCode EndIf Catch to loException ? loW.ExceptionMessage ? loW.StatusCode Endtry loBridge.Unload() Release loBridge