Sending Mail using a SMTP Socket

MSilva

New Member
Hi all,

We are using a progress program that sends mail using a SMTP Socket. It's fine, but know we have to attach a file on the message...

Does anybody know a way to do this (using SMTP socket)??

The program was written based on a SMTP Protocol doc. and follows:


/*---------------------------------------------------------------------------*/
DEFINE INPUT PARAMETER vSmtpServer AS CHARACTER FORMAT "X(50)" NO-UNDO.
DEFINE INPUT PARAMETER vTo AS CHARACTER FORMAT "X(50)" NO-UNDO.
DEFINE INPUT PARAMETER vFrom AS CHARACTER FORMAT "X(50)" NO-UNDO.
DEFINE INPUT PARAMETER vSubject AS CHARACTER FORMAT "X(50)" NO-UNDO.
DEFINE INPUT PARAMETER vBody AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER vdebugmode AS LOGICAL NO-UNDO.
DEFINE OUTPUT PARAMETER vErro AS CHARACTER NO-UNDO.

/*
Status:
0 - No Connection to the server
1 - Waiting for 220 connection to SMTP server
2 - Waiting for 250 OK status to start sending email
3 - Waiting for 250 OK status for sender
4 - Waiting for 250 OK status for recipient
5 - Waiting for 354 OK status to send data
6 - Waiting for 250 OK status for message received
7 - Quiting
*/

DEFINE VARIABLE vState AS INTEGER NO-UNDO.
DEFINE VARIABLE vbuffer AS MEMPTR NO-UNDO.
DEFINE VARIABLE hSocket AS HANDLE NO-UNDO.
DEFINE VARIABLE vstatus AS LOGICAL NO-UNDO.

RUN sendmail .



/*--------------------------------------------------------------------------*/
/* Internal Procedures */
PROCEDURE cleanup:
hSocket:DISCONNECT() NO-ERROR.
DELETE OBJECT hSocket NO-ERROR.
END.
/*---------------------------------------------------------------------------*/
PROCEDURE sendmail:
CREATE SOCKET hSocket.
hSocket:SET-READ-RESPONSE-PROCEDURE ("readHandler",THIS-PROCEDURE).
vstatus = hSocket:CONNECT("-S 25 -H " + vSmtpServer) NO-ERROR.
/*vstatus = hSocket:CONNECT("-S 25") NO-ERROR.*/

IF NOT vstatus THEN DO:
ASSIGN vErro = vErro + "Server not avail.".
RETURN.
END.
vstate = 1.
REPEAT ON STOP UNDO, LEAVE ON QUIT UNDO, LEAVE:
IF vstate < 0 OR vstate = 7 THEN LEAVE.
WAIT-FOR READ-RESPONSE OF hSocket.
END.
RUN cleanup.
END.
/*--------------------------------------------------------------------------*/
PROCEDURE newState:
DEFINE INPUT PARAMETER newState AS INTEGER.
DEFINE INPUT PARAMETER pstring AS CHARACTER.
IF vdebugmode THEN
vErro = vErro + "newState: " + STRING(newState) + " pstring: " + pstring.
vState = newState.
IF pstring = "" THEN RETURN.
SET-SIZE(vbuffer) = LENGTH(pstring) + 1.
PUT-STRING(vbuffer,1) = pstring.
hSocket:WRITE(vbuffer, 1, LENGTH(pstring)).
SET-SIZE(vbuffer) = 0.
END.
/*---------------------------------------------------------------------------*/
PROCEDURE readHandler:
DEFINE VARIABLE vlength AS INTEGER NO-UNDO.
DEFINE VARIABLE str AS CHARACTER NO-UNDO.
DEFINE VARIABLE v AS INTEGER NO-UNDO.
vlength = hSocket:GET-BYTES-AVAILABLE().
IF vlength > 0 THEN DO:
SET-SIZE(vbuffer) = vlength + 1.
hSocket:READ(vbuffer, 1, vlength, 1).
str = GET-STRING(vbuffer,1).
IF vdebugmode THEN
vErro = vErro + "server:" + str.
SET-SIZE(vbuffer) = 0.
v = INTEGER(ENTRY(1, str," ")).
CASE vState:
WHEN 1 THEN DO:
IF v = 220 THEN
RUN newState(2, "HELO how are you? ~n").
ELSE vState = -1.
END.
WHEN 2 THEN DO:
IF v = 250 THEN
RUN newState(3, "MAIL From: " + vFrom + "~n").
ELSE vState = -1.
END.
WHEN 3 THEN DO:
IF v = 250 THEN
RUN newState(4, "RCPT TO: " + vTo + "~n").
ELSE vState = -1.
END.
WHEN 4 THEN DO:
IF v = 250 THEN
RUN newState(5, "DATA ~n").
ELSE vState = -1.
END.
WHEN 5 THEN DO:
IF v = 354 THEN
RUN newState(6, "From: " + vFrom + " ~n" +
"To: " + vTo + " ~n" +
"Subject: " + vSubject +
" ~n~n" +
vBody + "~n" +
".~n").
ELSE vState = -1.
END.
WHEN 6 THEN DO:
IF v = 250 THEN
RUN newState(7,"QUIT~n").
ELSE vState = -1.
END.
END CASE.
END.
IF vState = 7 AND vdebugmode THEN
vErro = vErro + "Email accept.".
IF vState < 0 THEN
vErro = vErro + "Email rejected ".
END.
/*--------------------------------------------------------------------------*/


Thanks

Msilva
 
Hi MSilva!

We use MSMAPI32.OCX

If you are interrested I can send you our sendmail.w and sendmail.wrx sourcecode + MSMAPI32.OCX.

:cool:
 
Hi

Thanks Tranborg, but Chris Lawry have already sent to us a pretty good version of a mailsocket program (including ATTACH feature).

If you want to see the code let me know...


Mauricio Silva
 
Back
Top