Re: Email using .NET

Author: Alan Bourke

Posted: 2016-03-30 at 07:25:30

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

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/profox

OT-free version of this list: http://mail.leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/1459340730.3223457.563514594.25C26138@webmail.messagingengine.com

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2016 Alan Bourke