wwSMTP::SendMail

This method allows sending of SMTP emails which is the most common Email interface for the Internet. This method requires that you have access to an SMTP mail server.

This method sends a message in one step by connecting to the server, sending the message and disconnection. To use it set the properties of the wwSmtp object.

To send email set various property values and send the actual message. The following are a list of the most common properties that need to be set to send a message:

cMailServer
The IP Address or domain name of the SMTP server that is responsible for sending the message. The server must support 'pure' SMTP mail services and must not require a login prior to sending messages.

cSendername
Display name of the person sending the message. This option is often overridden by the mail server with the actual user account, but some servers leave the name as set here.

cSenderEmail
The sender's Email address.

cRecipient
Either a single recipient email address or a list of comma delimited addresses. Limited to 1024 characters. To specify a name and email address use the following syntax (works for all Recipient and CC properties): Joe Doe jdoe@yourserver.com

cCCList
Either a single CC recipient email address of a list of comma delimited addresses. Limited to 1024 characters.

cBCCList
Blind CCs are sent messages but do not show on the recipient or CC listing of the receiver. Limited to 1024 characters.

cSubject
The message subject or title.

cMessage
The actual message body text.

cAttachment
The name of the file that is to be attached to the message. This property must be filled with a valid path pointing at the file to attach. Multiple files can be attached by separating each file with a comma.

cContentType
Content type of the message allows you to send HTML messages for example. For HTML set the content type to text/html. Content types only work with text content types at this time since special encoding of the message text is not supported. All binary types should be sent as attachments.

cUsername, cPassword
If you are sending mail to a mail server that requires username and password authentication, set these properties. Only unencrypted, plain text communication is supported using the AUTH LOGIN command.

o.SendMail()

Return Value

.T. or .F. If the result is .F. check the cErrorMsg property for the error message.

Remarks

If you're sending plain text messages separate paragraphs with CHR(13) + CHR(10). Remember that if you send HTML you will need
and

tags to separate your lines (or use DisplayMemo() from wwUtils).

SendMail works with pure SMTP servers and most ESMTP servers. However, some servers that don't properly support these interfaces may cause problems.

Common Problems

Due to email's potential as a spam agent there are scenarios where mail servers and your computer might get in the way of sending emails.

Relay Errors
Relay errors occur on the server and usually tell you that relaying is not supported or that your IP Address to send from is invalid for this server. Most mail servers these days do not allow sending email except from their specific set of known local domains. If you use their servers from an IP that is not in a known block (such as when you're travelling or have otherwise 'roaming' IP addresses), you will get an error. Most mail servers support Authentication which you can use by setting the cUsername and cPassword properties, or by first logging into the associated POP3 server with a user name and password (which you can do with the wwPop3 class). The latter is known as Read Before Send. Authenticated email has become nearly ubitquitous so you can pretty much assume that username and passwords are required for email access.

Anti-Virus Software or Firewall blocking
Because of security concerns in today's software there are many ways that email from a typical client machine can be blocked. Make sure that your (or your client's) Windows Firewall is set up to allow access for your application through port 25 which is the default SMTP port (or whatever other port you might be explicitly using). Some Anti-Virus software like McAffee also monitors TCP/IP ports in use and requires that you configure each application that uses a specific port.

Recipient, CC and BCC length limits
Many people want to effectively send bulk emails and it may seem like CC and BCC are an easy way to send many emails at once. There are a couple of problems with this: First each of the fields is limited to 2048 bytes individually so the amount of email addresses you can put onto a single email is limited. Second, it's highly recommended NOT to send emails using more than a couple of CC/BCC addresses as most spam filters will flag messages as spam and either remove it or at least flag it. If you care about delivery of your emails this should be a big concern. It's better to send email individually.

ISP Server Send Limits
In an effort to prevent spamming from their mail servers many ISP Mail servers prevent you from sending more than a fixed number of emails in a given time period. This means when you send an email blast it's quite possible that you hit this limit after which the mail server will return an error. If your emails are legit you can probably make arrangements with your ISP to allow additional mail limits. However, if you send large amounts of email it's often better to either use a dedicated mail server or a list server.

Example

DO wwSmtp 
LOCAL loSmtp as wwSmtp
loSmtp = CREATEOBJECT("wwSmtp")
loSmtp.nMailMode = 0  && .NET   2 - classic (default)

loSmtp.cMailServer = "smtp.gmail.com:587"
loSmtp.lUseSsl = .T.  && Google requires TLS

*** Optional authentication if server requires it
loSmtp.cUsername = "ricks"
loSmtp.cPassword = "secret"

loSmtp.cSenderName= "West Wind Tools"
loSmtp.cSenderEmail = "admin@west-wind.com"

loSmtp.cRecipient = "ricks22@hotmail.com,admin@mydomain.com"
loSmtp.cCcList = "admin2@mydomain.com,admin@monitor.com"

loSmtp.cSubject = "Test Message through Google"

*** Optional - custom content type - text/plain by default
loSmtp.cContentType = "text/html"
loSmtp.cMessage = "<b>Test message from wwsmtp, to check out how this operation works.</b>"

*** Optional - send an alternate format to display if HTML isn't supported
loSmtp.cAlternateContentType = "text/plain"
loSmtp.cAlternateText = "Here is alternate content in plain text."

*** Optionally attach files
loSmtp.AddAttachment("c:\rick4453.jpg","image/jpg")
loSmtp.AddAttachment("c:\sailbig.jpg","image/jpg")

*** Optional - a couple of options
loSmtp.cReplyTo = "james@cranky.com"
loSmtp.cPriority = "High"

*** Send it
llResult = loSmtp.Sendmail()
IF llResult
   WAIT WINDOW "Mail sent..." nowait
ELSE
	? "Mail sending failed: " 
	? loSmtp.cErrorMsg
ENDIF

See also:

Class wwSMTP

© West Wind Technologies, 1996-2018 • Updated: 11/29/16
Comment or report problem with topic