Sending SMTP Email

wwSmtp easily supports sending email over the Internet. Use the following syntax for using the mail functionality is the same:

DO wwSmtp && load libraries

LOCAL loSMTP as wwSmtp
loSmtp=CREATEOBJECT("wwSmtp")
loSmtp.nMailMode = 0  && wwIPStuff mode (Win32 - default)   0 - .NET wwSmtp

loSmtp.cMailServer="mail.yourmailserver.net"
loSmtp.cSenderEmail="rstrahl@mydomain.com"
loSmtp.cSenderName="Mr. Testa"

*** Optional Username
* loSmtp.cUsername = "ricks"
* loSmtp.cPassword = "secret"

*** Optional SSL Messages (only in .NET mode (nMailMode = 0))
* loSmtp.lUseSsl = .T.

loSmtp.cRecipient="somebody@sweat.com,another@bust.com"
loSmtp.cCCList="rstrahl@east-wind.com,looser@nobody.com"
loSmtp.cBCCList="fred@flintstone.com"
loSmtp.cSubject="wwSmtp Test Message"

*** Optionally specify content type - text/plain is default
loSmtp.cContentType = "text/html"  
loSmtp.cMessage="Who said this had to be <b>difficult</b>?"

*** Optionally provide an alternate content type or plain text fallback
loSmtp.cAlternateContentType = "text/plain"
loSmtp.cAlternateText = "Plain text can go here."

*** Optionally send file attachments
loSmtp.AddAttachment("c:\temp\somefile.pdf")
oSmtp.AddAttachment("c:\temp\sailbig.jpg")

llResult = loSmtp.[SendMail](vfps://Topic/wwSmtp%3A%3Asendmail)()      

IF !llResult
   Wait window loSmtp.cErrorMsg
ENDIF

You can also send messages Asynchronously:

*** Optionally send Asynchronous without waiting for a result
loSmtp.SendMailAsync()

Note that when you send messages asynchronously, you get no result code or other indication whether the mail sending was a success.

Dependencies for wwSmtp

External dependencies depend on the nMailMode property setting.

nMailMode = 0
.NET 2.0 or later Runtime installed wwDotNetBridge.dll (in Fox Path) wwIPStuff.dll (in Fox Path)

nMailMode = 2
wwIPstuff.dll (in Fox Path)

Errors and Logging

Sending Emails can cause many errors on the server if your connection isn't configured properly or if you're not providing the right security information. Many common errors can pop up. In general when an error occurs wwIPStuff returns the SERVER ERROR MESSAGE. This is important - these errors typically have a 3 digit error code and a message associated with that is displayed in raw format. The messages give the server's reason for not accepting or failing the opeation. This error message is returned to you in the cErrorMsg property which you can check after the SendMail() call:

IF !loSmtp.SendMail()
	wait window loSmtp.cErrorMsg
	RETURN
ENDIF

In addition to the error message you can also log the entire SMTP conversation with the server assuming you managed to connect in the first place. To see the session set the .cLogFileProperty to a filename that will act as your log file:

loSmtp.cLogFile = FULLPATH("SmtpLog.txt")
llResult = loSmtp.SendMail()

This is really what I want to convey. If you just type and go on, it's all fun and games. Until you have a problem.

The log might give further clues to why an SMTP session failed on the server.

Note: Logging is available only in .MailMode=2 (wwIPStuff). For .NET based logging use the built in .NET System.Tracing logging features.

See also

wwSMTP::SendMail | wwSMTP::SendMailAsync

© West Wind Technologies, 1996-2018 • Updated: 07/08/17
Comment or report problem with topic