View previous topic :: View next topic |
Author |
Message |
bibibo
Joined: 30 Nov 2009 Posts: 168
|
Posted: Thu Apr 24, 2014 9:44 pm Post subject: SMTP Protocol |
|
|
I try to make SMTP communication and i tested with two SMTP servers.
First SMTP server . no problem, working very good
Second SMTP server sends error after command Tcpwrite(idx ,"DATA{013}{010}")
If i send email by telnet, no error appears. I do not know what problem there is.
Code: |
idx = Getsocket(smtp_email_socket , Sock_stream , 4086 , 0)
If idx = 255 Then print "SMTP Email Getsocket Error"
'(
result = Socketstat(idx , Sel_control )
Select Case result
Case Sock_closed : print "Sock_closedr"
Case Sock_arp : print "SOCK_ARP"
Case Sock_listen : print "SOCK_LISTEN"
Case Sock_synsent : print "SOCK_SYNSENT"
Case Sock_synrecv : print "SOCK_SYNRECV"
Case Sock_established : print "SOCK_ESTABLISHED"
Case Sock_close_wait : print "SOCK_CLOSE_WAIT"
Case Sock_last_ack : print "SOCK_LAST_ACK"
Case Sock_fin_wait : print "SOCK_FIN_WAIT"
Case Sock_closing : print "SOCK_CLOSING"
Case Sock_time_wait : print "SOCK_TIME_WAIT"
Case Sock_init : print "SOCK_INIT"
Case Sock_udp : print "SOCK_UDP"
Case Sock_raw : print "SOCK_RAW"
Case Sock_macraw : print "SOCK_MACRAW"
Case Sock_ppoe : print "SOCK_PPOE"
End Select
')
'connect to smtp server
result = Socketconnect(idx , SMTP_server (1).SMTP_server (2).SMTP_server (3).SMTP_server (4), SMTP_port ) ' smtp server and SMTP port 25
' ^socket
' ^ ip address of the smtp server
' ^ port 25 for smtp
print "Connecting to Email Server: " ; str(SMTP_server (1)) ; "." ; str(SMTP_server (2)) ; "."; str(SMTP_server (3)) ; "." ; str(SMTP_server (4)) ; " Port: " ; str(SMTP_port )
print "Socketconnect Status = " ; result
If result = 1 Then print "ERROR connecting to " ; str(SMTP_server (1)) ; "." ; str(SMTP_server (2)) ; "."; str(SMTP_server (3)) ; "." ; str(SMTP_server (4)) ; " Port: " ; str(SMTP_port )
Do
result = Socketstat(idx , 0) ' get socket status
Loop Until result = Sock_established
print "SMTP = " ; "SOCK_ESTABLISHED"
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "220" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
print "Send " ; "HELO Peto{013}{010}"
Tcp_var = Tcpwrite(idx , "HELO Peto{013}{010}" ) ' send username
print Tcp_var ; " bytes written" ' number of bytes actual send
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "250" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
If Len(User ) > 0 Then
print "Send " ; "AUTH LOGIN{013}{010}"
Tcp_var = Tcpwrite(idx , "AUTH LOGIN{013}{010}" ) 'authentication login
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "334" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
Temp_string = Base64enc(User )
'The username must be Base64 encoded (and add the CR + LF)!
Tcp_var = Tcpwritestr(idx , Temp_string, 255 ) 'USERNAME
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "334" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
Temp_string = Base64enc(Pass )
'The password must be Base64 encoded (and add the CR + LF) !
Tcp_var = Tcpwritestr(idx , Temp_string, 255 ) 'PASSWORD
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "235" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
End If
'Your Email address
Tcp_var = Tcpwrite(idx , "MAIL FROM: testemail@test.com{013}{010}")
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "250" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
If instr(RCPT_TO_1, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_1 + "{013}{010}"
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If
If instr(RCPT_TO_2, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_2 + "{013}{010}"
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If
'Print Temp_string
'The Email address you want to send the message to
'Tcp_var = Tcpwritestr(idx , Temp_string, 255)
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "250" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
Tcp_var = Tcpwrite(idx , "DATA{013}{010}{013}{010}" )
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
Print email_string
If Left(email_string , 3) = "354" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
Tcp_var = Tcpwrite(idx , "SUBJECT: Remote Relay Control{013}{010}")
If Send_html = 1 Then
'----------------------Send the Email as HTML ---------------------------
Tcp_var = Tcpwrite(idx , "Mime-Version: 1.0;{013}{010}")
Tcp_var = Tcpwrite(idx , "Content-Type: text/html; charset={034}ISO-8859-1{034};{013}{010}")
Tcp_var = Tcpwrite(idx , "Content-Transfer-Encoding: 7bit;{013}{010}")
Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch to body
Tcp_var = Tcpwrite(idx , "<html>{013}{010}")
Tcp_var = Tcpwrite(idx , "<body>{013}{010}")
Tcp_var = Tcpwrite(idx , "<h2>Remote Relay Control</h2>{013}{010}")
Tcp_var = Tcpwrite(idx , "</body>{013}{010}")
Tcp_var = Tcpwrite(idx , "</html>{013}{010}")
Tcp_var = Tcpwrite(idx , ".{013}{010}")
Else
'-----------------------Send only TEXT-----------------------------------
Tcp_var = Tcpwrite(idx , "{013}{010}") 'Empty line to switch from subject to body
Tcp_var = Tcpwrite(idx , "Toto je test{013}{010}")
Tcp_var = Tcpwrite(idx , "Ahoj{013}{010}")
length = len(email_string )
Tcp_var = Tcpwrite(idx , email_string, length )
Tcp_var = Tcpwrite(idx , ".{013}{010}") ' end with a single dot
end if
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "250" Then
print "Server Answer = " ; email_string '--> 250 2.6.0 Message accepted
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
print "Send " ; "QUIT{013}{010}"
Tcp_var = Tcpwrite(idx , "QUIT{013}{010}")
Do
Tcp_var = Tcpread(idx , email_string ) ' get response from SMTP server
If Left(email_string , 3) = "221" Then
print "Server Answer = " ; email_string
Exit Do
End If
'The Watchdog will restart the micro in case of no or wrong answer
Loop
Print "Close Socket"
Closesocket Idx ' close the server connection |
|
|
Back to top |
|
|
MAK3
Joined: 24 Sep 2010 Posts: 449 Location: Germany
|
Posted: Fri Apr 25, 2014 7:02 am Post subject: |
|
|
Hi,
your command in the code is actually
Code: | Tcp_var = Tcpwrite(idx ,"DATA{013}{010}{013}{010}" ) |
Why two times {013}{010} ?
MAK3 |
|
Back to top |
|
|
bibibo
Joined: 30 Nov 2009 Posts: 168
|
Posted: Fri Apr 25, 2014 8:42 am Post subject: |
|
|
Sorry, it was test with two {013}{010}. It also does not work with one {013}{010} |
|
Back to top |
|
|
MAK3
Joined: 24 Sep 2010 Posts: 449 Location: Germany
|
Posted: Fri Apr 25, 2014 10:36 am Post subject: |
|
|
What is the server sending back after the DATA command ?
Once you sent the DATA command the server is waiting for input until you send the "."
Tcp_var = Tcpwrite(idx , ".{013}{010}")
Quote: | DATA to signal the beginning of the message text; the content of the message, as opposed to its envelope. It consists of a message header and a message body separated by an empty line. DATA is actually a group of commands, and the server replies twice: once to the DATA command proper, to acknowledge that it is ready to receive the text, and the second time after the end-of-data sequence, to either accept or reject the entire message. |
Maybe one Server is more accurate than the other or you need to change the timing (what to send when to this specific server)
Please check also:
http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
There are lots of reply codes that can come back from Server after DATA comand:
Quote: | DATA replay codes
354 Start mail input; end with <CRLF>.<CRLF>
451 Requested action aborted: local error in processing
554 Transaction failed
500 Syntax error, command unrecognised
501 Syntax error in parameters or arguments
503 Bad sequence of commands
421 <domain> Service not available, closing transmission channel
received data
250 Requested mail action okay, completed
552 Requested mail action aborted: exceeded storage allocation
554 Transaction failed
451 Requested action aborted: local error in processing
452 Requested action not taken: insufficient system storage |
|
|
Back to top |
|
|
bibibo
Joined: 30 Nov 2009 Posts: 168
|
Posted: Fri Apr 25, 2014 1:31 pm Post subject: |
|
|
Hello,
When atmega sends command Data, the mail server answers
500 5.5.2 Error: bad syntax
503 5.5.0 <DATA>: Data command rejected: Improper use of SMTP command pipelining
I do not know what this means. |
|
Back to top |
|
|
MAK3
Joined: 24 Sep 2010 Posts: 449 Location: Germany
|
|
Back to top |
|
|
bibibo
Joined: 30 Nov 2009 Posts: 168
|
Posted: Fri Apr 25, 2014 7:32 pm Post subject: |
|
|
Hello MAK3,
Thanks a lot. The problem was solved. The problem was in the code below.
Code: |
If instr(RCPT_TO_1, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_1 + "{013}{010}"
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If
If instr(RCPT_TO_2, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_2 + "{013}{010}"
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If |
The correct code is
Code: |
If instr(RCPT_TO_1, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_1
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If
If instr(RCPT_TO_2, "@") > 0 Then
Temp_string = "RCPT TO: " + RCPT_TO_2
Tcp_var = Tcpwritestr(idx , Temp_string, 255)
End If |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|