none
530 5.7.0 Must issue a STARTTLS command first

    Question

  •  

     

    I am trying to send email using smtp but getting this error

     

     

    530 5.7.0 Must issue a STARTTLS command first

     

    *********************************

    here is the code

    ***********************

    #DEFINE CrLf Chr(13)+Chr(10)

    CLEAR

    CLOSE TABLES ALL

    LOCAL cServer, cSender, cRecipient, obj

    cServer = "smtp.gmail.com"

    cSender = "punga.ait@gmail.com"

    cRecipient = "sur200@indiatimes.com"

    obj = CreateObject("Tsmtp", cServer, cSender, cRecipient)

    IF VARTYPE(obj) <> "O"

    = MessageB("Unable to initiaze Tsmtp object. " + Chr(13) +;

    "Check Host, Sender, and Recipient parameters. ", 48, " Error")

    ELSE

    WITH obj

    .subject = "Testing Winsock SMTP functionality"

    .body = "Test message:" + CrLf + CrLf

    .SendMail()

    ENDWITH

    IF USED("csLog")

    SELECT csLog

    GO TOP

    BROW NORMAL NOWAIT

    ENDIF

    ENDIF

    * end of main

    DEFINE CLASS Tsmtp As Custom

    #DEFINE SMTP_PORT 25 && default SMTP port

    #DEFINE AF_INET 2

    #DEFINE SOCK_STREAM 1

    #DEFINE IPPROTO_TCP 6

    #DEFINE SOCKET_ERROR -1

    #DEFINE FD_READ 1

    host=""

    IP=""

    sender=""

    recipient=""

    subject=""

    body=""

    hSocket=0

    PROCEDURE Init(cServer, cSender, cRecipient)

    DO decl

    IF WSAStartup(0x202, Repli(Chr(0),512)) <> 0

    * unable to initialize Winsock on this computer

    RETURN .F.

    ENDIF

    THIS.host = cServer

    THIS.sender = cSender

    THIS.recipient = cRecipient

    IF Not THIS.InitCheck()

    = WSACleanup()

    RETURN .F.

    ENDIF

    FUNCTION InitCheck

    IF EMPTY(THIS.host) Or EMPTY(THIS.recipient);

    Or EMPTY(THIS.sender)

    * invalid Host or sender/recipient email address

    RETURN .F.

    ENDIF

    THIS.IP = THIS.GetIP()

    MESSAGEBOX(this.ip)

    IF EMPTY(THIS.IP)

    * can not resolve Host name to IP address

    RETURN .F.

    ENDIF

    RETURN .T.

    PROCEDURE Destroy

    = WSACleanup()

    PROTECTED FUNCTION IsMailValid && just a minimal check

    RETURN Not (EMPTY(THIS.sender) Or EMPTY(THIS.recipient);

    Or EMPTY(THIS.subject+THIS.body))

    PROTECTED FUNCTION GetIP

    #DEFINE HOSTENT_SIZE 16

    LOCAL nStruct, nSize, cBuffer, nAddr, cIP

    nStruct = gethostbyname(THIS.host)

    IF nStruct = 0

    RETURN ""

    ENDIF

    cBuffer = Repli(Chr(0), HOSTENT_SIZE)

    cIP = Repli(Chr(0), 4)

    = CopyMemory(@cBuffer, nStruct, HOSTENT_SIZE)

    = CopyMemory(@cIP, buf2dword(SUBS(cBuffer,13,4)),4)

    = CopyMemory(@cIP, buf2dword(cIP),4)

    RETURN inet_ntoa(buf2dword(cIP))

    PROTECTED FUNCTION ConnectTo

    LOCAL cBuffer, cPort, cHost, lResult

    cPort = num2word(htons(SMTP_PORT))

    nHost = inet_addr(THIS.IP)

    cHost = num2dword(nHost)

    cBuffer = num2word(AF_INET) + cPort + cHost + Repli(Chr(0),8)

    lResult = (ws_connect(THIS.hSocket, @cBuffer, Len(cBuffer))=0)

    RETURN lResult

    FUNCTION SendMail

    IF Not THIS.IsMailValid()

    RETURN .F.

    ENDIF

    THIS.hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

    IF THIS.hSocket = SOCKET_ERROR

    RETURN .F.

    ENDIF

    LOCAL lResult

    IF THIS.ConnectTo()

    ** Most servers expect the server name after HELO:

    ** THIS.snd("HELO", .T.)

    THIS.snd("HELO "+THIS.host, .T.)

    THIS.snd("MAIL FROM:<" + THIS.sender + ">", .T.)

    THIS.snd("RCPT TO:<" + THIS.recipient + ">", .T.)

    THIS.snd("DATA", .T.)

    THIS.snd("From: " + THIS.sender)

    THIS.snd("To: " + THIS.recipient)

    THIS.snd("Subject: " + THIS.subject)

    THIS.snd("")

    THIS.snd(THIS.body)

    THIS.snd(".", .T.)

    THIS.snd("QUIT", .T.)

    lResult = .T.

    ELSE

    = MessageB("Unable to connect to [" + THIS.host +;

    "] on port " + LTRIM(STR(SMTP_PORT)) + ". ",;

    48, " Connection error")

    lResult = .F.

    ENDIF

    = closesocket(THIS.hSocket)

    RETURN lResult

    ENDFUNC

    PROTECTED FUNCTION snd(cData, lResponse)

    THIS.writelog(1, cData)

    LOCAL cBuffer, nResult, cResponse

    cBuffer = cData + CrLf

    nResult = send(THIS.hSocket, @cBuffer, Len(cBuffer), 0)

    IF nResult = SOCKET_ERROR

    RETURN .F.

    ENDIF

    IF Not lResponse

    RETURN .T.

    ENDIF

    LOCAL hEventRead, nWait, cRead

    DO WHILE .T.

    * creating event, linking it to the socket and wait

    hEventRead = WSACreateEvent()

    = WSAEventSelect(THIS.hSocket, hEventRead, FD_READ)

    * 1000 milliseconds can be not enough

    nWait = WSAWaitForMultipleEvents(1, @hEventRead,;

    0, 1000, 0)

    = WSACloseEvent(hEventRead)

    IF nWait <> 0 && error or timeout

    EXIT

    ENDIF

    * reading data from connected socket

     

    cRead = THIS.rd()

    IF Not EMPTY(cRead)

    THIS.writelog(0, cRead)

    ENDIF

    ENDDO

    RETURN .T.

    PROTECTED FUNCTION rd

    #DEFINE READ_SIZE 16384

    LOCAL cRecv, nRecv, nFlags

    cRecv = Repli(Chr(0), READ_SIZE)

    nFlags = 0

    nRecv = recv(THIS.hSocket, @cRecv, READ_SIZE, nFlags)

    RETURN Iif(nRecv<=0, "", LEFT(cRecv, nRecv))

    PROTECTED PROCEDURE writelog(nMode, cMsg)

    IF Not USED("csLog")

    CREATE CURSOR csLog(dir I, msg C(250))

    ENDIF

    ? cMsg

    cMsg = CrLf + cMsg + CrLf

     

    LOCAL nIndex, nPos0, nPos1

    nIndex = 1

    DO WHILE .T.

    nPos0 = AT(CrLf, cMsg, nIndex)

    nPos1 = AT(CrLf, cMsg, nIndex+1)

    IF nPos1 = 0

    EXIT

    ENDIF

    cLog = SUBSTR(cMsg, nPos0, nPos1-nPos0)

    cLog = STRTRAN(STRTRAN(cLog, Chr(13),""),Chr(10),"")

    IF Not EMPTY(cLog)

    INSERT INTO csLog VALUES (m.nMode, m.cLog)

    ENDIF

    nIndex = nIndex + 1

    ENDDO

    ENDDEFINE

    PROCEDURE decl

    DECLARE INTEGER gethostbyname IN ws2_32 STRING host

    DECLARE STRING inet_ntoa IN ws2_32 INTEGER in_addr

    DECLARE INTEGER socket IN ws2_32 INTEGER af, INTEGER tp, INTEGER pt

    DECLARE INTEGER closesocket IN ws2_32 INTEGER s

    DECLARE INTEGER WSACreateEvent IN ws2_32

    DECLARE INTEGER WSACloseEvent IN ws2_32 INTEGER hEvent

    DECLARE GetSystemTime IN kernel32 STRING @lpSystemTime

    DECLARE INTEGER inet_addr IN ws2_32 STRING cp

    DECLARE INTEGER htons IN ws2_32 INTEGER hostshort

    DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING lpWSAData

    DECLARE INTEGER WSACleanup IN ws2_32

    DECLARE INTEGER connect IN ws2_32 AS ws_connect ;

    INTEGER s, STRING @sname, INTEGER namelen

    DECLARE INTEGER send IN ws2_32;

    INTEGER s, STRING @buf, INTEGER buflen, INTEGER flags

    DECLARE INTEGER recv IN ws2_32;

    INTEGER s, STRING @buf, INTEGER buflen, INTEGER flags

    DECLARE INTEGER WSAEventSelect IN ws2_32;

    INTEGER s, INTEGER hEventObject, INTEGER lNetworkEvents

    DECLARE INTEGER WSAWaitForMultipleEvents IN ws2_32;

    INTEGER cEvents, INTEGER @lphEvents, INTEGER fWaitAll,;

    INTEGER dwTimeout, INTEGER fAlertable

    DECLARE RtlMoveMemory IN kernel32 As CopyMemory;

    STRING @Dest, INTEGER Src, INTEGER nLength

    FUNCTION buf2dword(lcBuffer)

    RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;

    BitLShift(Asc(SUBSTR(lcBuffer, 2,1)), 8) +;

    BitLShift(Asc(SUBSTR(lcBuffer, 3,1)), 16) +;

    BitLShift(Asc(SUBSTR(lcBuffer, 4,1)), 24)

    FUNCTION num2dword(lnValue)

    #DEFINE m0 256

    #DEFINE m1 65536

    #DEFINE m2 16777216

    IF lnValue < 0

    lnValue = 0x100000000 + lnValue

    ENDIF

    LOCAL b0, b1, b2, b3

    b3 = Int(lnValue/m2)

    b2 = Int((lnValue - b3*m2)/m1)

    b1 = Int((lnValue - b3*m2 - b2*m1)/m0)

    b0 = Mod(lnValue, m0)

    RETURN Chr(b0)+Chr(b1)+Chr(b2)+Chr(b3)

    FUNCTION num2word(lnValue)

    RETURN Chr(MOD(m.lnValue,256)) + CHR(INT(m.lnValue/256))

    **************************

     

     

    Please help if anybody knows.

     

    Thanks

     

    Wednesday, October 03, 2007 7:00 AM

Answers

  • No body knows how to solve this, no problem I have found an alternate way to this(I am using CDO know).

    But if anyone knows about this then please reply.

     

     

     

    Thanks

     

     

    Tuesday, October 09, 2007 4:19 AM

All replies

  • Please help how to start tls using foxpro.

     

     

    • Proposed as answer by zivotije Sunday, June 12, 2011 9:46 AM
    Thursday, October 04, 2007 6:52 AM
  • No body knows how to solve this, no problem I have found an alternate way to this(I am using CDO know).

    But if anyone knows about this then please reply.

     

     

     

    Thanks

     

     

    Tuesday, October 09, 2007 4:19 AM
  • I am also experciencing this issue. I've just upgraded from Office 2003 to Office 2007. I hope someone can provide a fix.

    Thanks.
    • Proposed as answer by zivotije Sunday, June 12, 2011 9:44 AM
    • Unproposed as answer by zivotije Sunday, June 12, 2011 9:46 AM
    Saturday, November 17, 2007 1:31 PM
  • Turn off TLS encryption on the SMTP virtual server. It is turned on by default.

     

    Monday, January 14, 2008 6:22 AM
  • hi guys, you must set a wrong SMTP port, the correct Gmail SMTP port is 465,try it again。
    Friday, September 10, 2010 1:01 AM