How to send e-mail directly from your program
Contents
Overview
What
is SMTP?
Why
Should I Talk Directly to an SMTP Server?
A
Diagram of how it all works
But
I need to send Attachments and use HTML mail?
This
sounds complicated, is there an easier way?
Does
SMTP Wizard support Secure Socket Layer?
I
want to send the message Asynchronously so I can do other things
Could
you show some sample code to make me believe how simple it is?
Where
can I find this program?
Who
is Seekford Solutions, Inc.?
Overview
A question that I have seen being
asked a lot lately is “How do I get around that annoying Outlook Security
Dialog?” Well, I am here to answer that question and much more. In this article
you will learn how to send email completely bypassing MAPI and Outlook as well
as use powerful features like HTML mail and attachments.
What
is SMTP?
SMTP is the protocol used to
deliver mail. It is an acronym for Simple Mail Transfer Protocol. Simple is a
rather misleading term since with all the knowledge needed to make and send
e-mails the protocol gets rather complex. SMTP servers are what every email
program interfaces with to send their email. A client connects to his ISP’s
SMTP server and initiates a mail send. Once that server has received the
message it queues and then sends the message to the next server in the chain
until it reaches the actual mail server for the recipient. If you have ever
looked at the headers for a message in your inbox, you probably noticed all the
Received-From header entries showing you all the servers your mail bounced
through before it got to you.
Why
Should I Talk Directly to an SMTP Server?
Email clients have built in
protocol handlers for communicating and delivering their messages to your mail
server. Their software is intended for a user to type their message within that
application and send mail. The main focus is not automation for email client
vendors nor should you use them that way. If they ever decide to implement more
security restrictions, or even just take away the automation interface then
your application is completely unable to send email. However, if you decide
that you will talk directly with the mail server, you then bypass any other
mail client and never have to depend on them for the mail delivery. The only
security restrictions would be those set by the mail server itself and that is
much easier to control.
Another important item to note is
that status you get. Since you are directly communicating the message
real-time, you get real-time status notifications. If a recipient is rejected,
the message can’t go through immediately, the password or username is incorrect
or whatever the error, you can handle it. You are not guaranteed delivery
status or even delivery times when using other clients.
A
diagram of how it all works:

This is a simplified diagram not
including the hurdles reached it the MAPI program decides it want to popup
Security Prompts or other roadblocks to automation.
But I
need to send Attachments and use HTML mail?
The Simple Mail Transport Protocol
has nothing to do with the content of your messages. It is only a delivery
mechanism for sending Internet mail regardless of the format. The only
restriction is that he message must follow a standard for its header and body
formats defined in RFC 822 (Standard for the Format of ARPA Internet Text
Messages) which is a document that outlines the guidelines for how message must
be formatted. Now this has nothing to do with data content, that is when we get
to the RFC 2045, RFC 2046, RFC 2047, RFC 2048, and RFC 2049 which define how
create MIME or Multipart Internet Messages. This is a very comprehensive set of
documentation, which takes about two days just to read and then much more to
understand. With the information
in those documents, you can build complex MIME messages that allow for
Attachments, HTML mail and more.
This
sounds complicated, is there an easier way?
Yes there is. Seekford Solutions,
Inc. has created a very powerful ActiveX control that makes sending email
directly to a SMTP server a snap. With only a few lines of code you are on your
way. The main control provides the support for the main Simple Mail Transport
Protocol but leaves all the complexity out of it for you. The secondary object
is an SMTP Message object which implements all those RFC standards listed
above. With methods as simple as AddAttachmentByFilename and a property as
simple as HTMLBody you can send emails with attachments, HTML or plain text,
have multiple recipients and even Blind Carbon Copy.
Does
SMTP Wizard support Secure Socket Layer?
One new feature of the SMTP Wizard
program is that it supports Secure Socket Layer connections so if you upgrade
you mail server to this level of security, you will already be ready for the
change. The only thing you need to do is set the UseSSL property to TRUE and
your done The control handles the rest.
I want to
send the message Asynchronously so I can do other things
Not a problem, SMTP Wizard SSL has
full support for asynchronous operation. You can use the Events that the
control fires for your status notifications and command processing. Another
great feature built-in is that the control even when in synchronous mode will
not freeze your application. Even during large message transmissions your
status windows and program will never freeze up to the user. This makes your
program look much more professional and robust. Not to mention allows you to
continue to receive windows messages without hanging.
Could
you show some sample code to make me believe how simple it is?
Sure here is code in a few different languages. A lot of the code below has
extra lines to demonstrate features.
[Visual
Basic][VBScript]
------------------------------------------------------------------------------------------------------------
dim
sMailServer
dim
iMailServerPort
dim
sUserName
dim
sPassword
dim
oSMTP
dim
oMessage
sMailServer
= "mail.YourServer.com"
' NOTE:
Set to your Own Value
iMailServerPort
= 25
' NOTE:
Already set to default value
sUserName
= "UserName"
' NOTE:
Set to your Own Value
sPassword
= "UserPassword"
' NOTE:
Set to your Own Value
set
oSMTP=CREATEOBJECT("SmtpWizard.SmtpWizardCtrl2")
oSMTP.UnlockSmtpWizard("YourSerialNumber")
' Your own serial# eliminates the eval popup window
oSMTP.FailOnRecipientErrors=
true
oSMTP.TimeOut=60000
oSMTP.UseSecurePasswordAuthentication=true
' IF YOU DONT USE LOGIN USER NAME AND PASSWORD,
SET TO FALSE
oSMTP.UseESMTP=true
oSMTP.MailServerPort=iMailServerPort
oSMTP.MailServer=sMailServer
msgbox
oSMTP.Version
'*---
Connect
if
oSMTP.Connect(sUserName,sPassword) then
IF oSMTP.LoggedInWithESMTP then
'ESMTP'
end if
IF oSMTP.LoginWasSecure then
'Login was secure
ELSE
'Unsecure login
end if
set oMessage = CREATEOBJECT("SmtpWizard.SMTPMessage2")
oMessage.fieldFROM = """My Display Name""
<SMTPTest@SeekfordSolutions.com>"
' NOTE:
Set to your Own Value
call oMessage.AddTORecipient ("User1","User1@User1Domain.com")
' NOTE:
Set to your Own Value
call oMessage.AddTORecipient ("User2","User2@User2Domain.com")
' NOTE:
Set to your Own Value
call oMessage.AddCCRecipient ("User3","User3@User3Domain.com")
' NOTE:
Set to your Own Value
call oMessage.AddBCCRecipient("User4","User4@User4Domain.com")
' NOTE:
Set to your Own Value
oMessage.fieldSubject = "This is a sample email with SMTP Wizard"
oMessage.MessageBody = "This is the body of the message. Isn't this
easy"
call oMessage.AddAttachmentByFileName("C:\autoexec.bat")
'-- sending message
If oSMTP.SendMessage(oMessage, 1) then
MsgBox "Message sent successfully"
end if
oSMTP.Disconnect
Else
MsgBox"Error: " +
oSMTP.LastErrorDescription
END
IF
------------------------------------------------------------------------------------------------------------
[Visual
C++]
------------------------------------------------------------------------------------------------------------
CString
sMailServer;
CString
iMailServerPort;
CString
sUserName;
CString
sPassword;
CSMTPWizard*
oSMTP = m_SMTP; //m_SMTP is a member variable of the
dialog
CSMTPMessage*
oMessage = new CSMTPMessage();
sMailServer
= "mail.YourServer.com";
// NOTE:
Set to your Own Value
iMailServerPort
= 25;
// NOTE:
Already set to default value
sUserName
= "UserName";
// NOTE:
Set to your Own Value
sPassword
= "UserPassword";
// NOTE:
Set to your Own Value
oSMTP->UnlockSmtpWizard("YourSerialNumber");
// Your own serial# eliminates the eval popup window
oSMTP->SetFailOnRecipientError(true);
oSMTP->SetTimeOut(60000);
oSMTP->SetUseSecurePasswordAuthentication(true);
// IF YOU DONT USE LOGIN USER NAME AND PASSWORD, SET TO FALSE
oSMTP->SetUseESMTP(true);
oSMTP->SetMailServerPort(iMailServerPort);
oSMTP->SetMailServer(sMailServer);
//*---
Connect
if(
oSMTP->Connect(sUserName,sPassword))
{
if( oSMTP->GetLoggedInWithESMTP())
{
//ESMTP//
}
if( oSMTP->GetLoginWasSecure())
{
//Login was secure
}else
{
//Unsecure login
}
oMessage->SetFieldFROM ( """My Display Name""
<SMTPTest@SeekfordSolutions->com>");
// NOTE:
Set to your Own Value
oMessage->AddTORecipient ("User1","User1@User1Domain->com");
// NOTE:
Set to your Own Value
oMessage->AddCCRecipient ("User3","User3@User3Domain->com");
// NOTE:
Set to your Own Value
oMessage->AddBCCRecipient("User4",User4@User4Domain->com);
// NOTE:
Set to your Own Value
oMessage->SetFieldSubject ( "This is a sample email with SMTP
Wizard");
oMessage->SetMessageBody ( "This is the body of the message in Plain
Text. Isn’t this easy");
oMessage->SetHTMLBody ( "<HTML><BODY>This is the body of
the message.<B> Isn.t this easy</B></BODY></HTML>");
oMessage->AddAttachmentByFileName("C:\autoexec->bat");
//-- sending message
If oSMTP->SendMessage(oMessage, 1)
{
AfxMessageBox("Message sent successfully");
}
}else
{
AfxMessageBox("Error: " +
oSMTP->GetLastErrorDescription());
}
oSMTP->Disconnect()
------------------------------------------------------------------------------------------------------------
[Active
Server Pages]
------------------------------------------------------------------------------------------------------------
<%
if
request.form("m_MailServer") <> "" then
dim MySMTPWizard
set MySMTPWizard =
server.CreateObject("SMTPWIZARD.SMTPWizardCtrl2")
Response.write("SMTP Timeout:" + CStr(MySMTPWizard.Timeout) + vbcrlf)
Response.write("SMTP Mail Server:""" + request.form("m_MailServer") +
"""" + vbcrlf)
Response.write("<HR>")
MySMTPWizard.UnlockSMTPWizard("") 'NOTE: YOU
WILL NEED A LICENSED COPY TO USE THIS IN Active Server Pages
Response.flush
Response.Write("<br>Connecting to mail server...<br>")
MySMTPWIZard.UseESMTP = true
if len(Request("m_bUseSPA")) = 0 then
MySMTPWIzard.UseSecurePasswordAuthentication = false
else
MySMTPWIzard.UseSecurePasswordAuthentication = true
end if
MySMTPWizard.MailServer = Request("m_MailServer")
MySMTPWizard.MailServerPort = request("m_port")
if (MySMTPWizard.Connect(Request("m_User"),Request("m_Password"))) then
Response.Write(MySMTPWizard.ServerSupportInformation +
"<br>Connected! Now Sending Mail!<br>")
dim MyMessage
set MyMessage = CreateObject("SMTPWIZARD.SMTPMessage2")
MyMessage.FieldFrom = Request("m_From")
MyMessage.FieldBCC = Request("m_BCC")
MyMessage.FieldCC = Request("m_CC")
MyMessage.FieldTO= Request("m_To")
MyMessage.FieldSubject = Request("m_Subject")
MyMessage.MessageBody = Request("m_MessageBody")
if (MySMTPWizard.SendMessageByText( MyMessage.FullMessage ,1)) then
Response.Write("Message Sent Successfully")
else
Response.Write("Error:" +MySMTPWizard.LastErrorDescription)
end if
else
Response.Write("Error:" +MySMTPWizard.LastErrorDescription)
end if
MySMTPWizard.MailServer = request.form("p_MailServer")
MySMTPWizard.Disconnect()
end
if
MailServer = Request("m_MailServer")
%>
------------------------------------------------------------------------------------------------------------
[Visual
FoxPro]
------------------------------------------------------------------------------------------------------------
local
sMailServer
local
iMailServerPort
local
sUserName
local
sPassword
local
oSMTP
local
oMessage
sMailServer
= THISFORM.txtMailServer.value &&
"mail.YourServer.com"
&& NOTE: Set to
your Own Value
iMailServerPort
= 25
&& NOTE:
Already set to default value
sUserName
= 'UserName'
&& NOTE:
Set to your Own Value
sPassword
= 'UserPassword'
&& NOTE:
Set to your Own Value
oSMTP=CREATEOBJECT("SmtpWizard.SmtpWizardCtrl2")
?oSMTP.Version
oSMTP.UnlockSmtpWizard("YourSerialNumber")
&& Your own serial# eliminates the eval popup
window
oSMTP.FailOnRecipientErrors=.t.
oSMTP.TimeOut=60000
oSMTP.UseSecurePasswordAuthentication=.t.
&& IF YOU DONT USE LOGIN USER NAME AND
PASSWORD, SET TO FALSE
oSMTP.UseESMTP=.t.
oSMTP.MailServerPort=iMailServerPort
oSMTP.MailServer=sMailServer
&&*---
Connect
if
oSMTP.Connect(sUserName,sPassword)
IF
oSMTP.LoggedInWithESMTP
?'ESMTP'
endif
IF
oSMTP.LoginWasSecure
?'Login
was secure'
ELSE
?'Unsecure
login'
endif
oMessage
= CREATEOBJECT("SmtpWizard.SMTPMessage2")
oMessage.fieldFROM
= ["My Display Name" <SMTPTest@SeekfordSolutions.com>]
&& NOTE:
Set to your Own Value
oMessage.AddTORecipient
("User1","User1@User1Domain.com")
&& NOTE:
Set to your Own Value
oMessage.AddTORecipient
("User2","User2@User2Domain.com")
&& NOTE:
Set to your Own Value
oMessage.AddCCRecipient
("User3","User3@User3Domain.com")
&& NOTE:
Set to your Own Value
oMessage.AddBCCRecipient("User4","User4@User4Domain.com")
&& NOTE:
Set to your Own Value
oMessage.fieldSubject
= [This is a sample email with SMTP Wizard]
oMessage.MessageBody
= [This is the body of the message. Isn't this easy]
oMessage.AddAttachmentByFileName("C:\Config.sys")
&&*--
sending message
If
oSMTP.SendMessage(oMessage, 1)
MessageBox('Message
sent successfully')
Else
MessageBox('Error:
'+oSMTP.LastErrorDescription)
ENDIF
oSMTP.Disconnect
else
MessageBox(oSMTP.LastErrorDescription)
endif
------------------------------------------------------------------------------------------------------------
Where
can I find this program?
It is available from Seekford
Solutions, Inc. at their website of
http://www.SeekfordSolutions.com/Products/SMTPWizard
Who
is Seekford Solutions, Inc.?
Seekford Solutions, Inc. is a
software development corporation specializing in the design and development of
state of the art ActiveX controls and custom projects. Their core product line
is focused on Internet technologies primarily in the facilitation of the use of
the common Internet protocols. The design philosophy is based on ease of use
and quick implementation time. They also handle custom projects for clients who
are need of specialty software or who need a framework base to use. The company
was founded in early 2001. Their website is
http://www.SeekfordSolutions.com.
|