SMTP with .NET Micro Framework
#1
Posted 26 April 2011 - 04:47 PM
I know the .net micro framework does not natively support SMTP like the full framework, but was wondering if anyone has implemented this in any way. I have seen this, but am unable to get it to work. I do not believe it supports SSL, which my gmail provider requires. Any ideas, or am is it a lost cause?
Thanks!
Mike
#2
Posted 26 April 2011 - 07:04 PM
SSL in the micro framework would be difficult; it's a big protocol to support and we got little RAM. See also: http://forums.netdui...security-class/
Normal SMTP shouldn't be too hard. I haven't got a Plus but I imagine opening a socket and writing ASCII data wouldn't be difficult.
Connect to a SMTP-server (of your local ISP?) on port 25 and you can send a mail:
HELO localhost
MAIL FROM:<email@ddre.ss>
RCPT TO:<recipient@ddre.ss>
DATA
From: <email@ddre.ss> "My name"
To: <recipient@ddre.ss> "Your name"
Subject: This is a real email message
Hi, this is the first line of the message
This the second,
etcetera.
We close with a single dot on a line.
Keep in mind that all lines should end with CrLf (ASCII 13 + ASCII 10).
.
QUIT
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#3
Posted 27 April 2011 - 12:53 AM
#4
Posted 27 April 2011 - 01:18 PM
I was messing around with it yesterday but had the same issue you are. I'm working on the socket method now. I don't need authentication for my particular project.I am having trouble getting the Bansky SMTP to work also, regardless of it not supporting SSL.
It compiles OK, runs OK under emulation, but after uploading it to the N+ it generates non-descript SMTPclientclass Exceptions.
Upon closer examination, it appears that the Netduino is not receiving a response from the SMTP Server, yet the socket connection appears to be succeeding.
Has anyone managed to get this running with their N+?
#5
Posted 27 April 2011 - 04:31 PM
#6
Posted 28 April 2011 - 12:11 AM
I was messing around with it yesterday but had the same issue you are. I'm working on the socket method now. I don't need authentication for my particular project.
Let us know if you get something working. I can find another route that does not require authentication.
#7
Posted 29 April 2011 - 12:07 PM
Yea, I'm not having much luck with sockets either. I tried the following code. It says it works successfully but the email never goes.Let us know if you get something working. I can find another route that does not require authentication.
Socket smtp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); smtp.Connect(new IPEndPoint(mailip, 25)); smtp.Send(Encoding.UTF8.GetBytes("HELO localhost")); smtp.Send(Encoding.UTF8.GetBytes("MAIL FROM: test@domain.com")); smtp.Send(Encoding.UTF8.GetBytes("RCPT TO: myemail@domain.com")); smtp.Send(Encoding.UTF8.GetBytes("DATA")); smtp.Send(Encoding.UTF8.GetBytes("Subject: Test Email")); smtp.Send(Encoding.UTF8.GetBytes("This is just a test!")); smtp.Send(Encoding.UTF8.GetBytes(" . ")); smtp.Send(Encoding.UTF8.GetBytes("QUIT")); smtp.Close();
#8
Posted 29 April 2011 - 01:04 PM
jmmille, As stated before I don't have a netduino plus, but I have some basic understandings of internet protocolsYea, I'm not having much luck with sockets either. I tried the following code. It says it works successfully but the email never goes.
Socket smtp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); smtp.Connect(new IPEndPoint(mailip, 25)); smtp.Send(Encoding.UTF8.GetBytes("HELO localhost")); smtp.Send(Encoding.UTF8.GetBytes("MAIL FROM: test@domain.com")); smtp.Send(Encoding.UTF8.GetBytes("RCPT TO: myemail@domain.com")); smtp.Send(Encoding.UTF8.GetBytes("DATA")); smtp.Send(Encoding.UTF8.GetBytes("Subject: Test Email")); smtp.Send(Encoding.UTF8.GetBytes("This is just a test!")); smtp.Send(Encoding.UTF8.GetBytes(" . ")); smtp.Send(Encoding.UTF8.GetBytes("QUIT")); smtp.Close();
Between the headers (Subject) and the mail body there must be an extra CrLf. also, every line should be appended with CrLf and the spaces around the closing dot should be removed. I tried this, and this gives the correct output:
string MailData = ""; MailData += "HELO localhost\r\n"; MailData += "MAIL FROM: test@domain.com\r\n"; MailData += "RCPT TO: myemail@domain.com\r\n"; MailData += "DATA\r\n"; MailData += "Subject: Test Email\r\n"; MailData += "From: \"My Name\" <test@domain.com>\r\n"; MailData += "To: \"your name\" <myemail@domain.com\r\n"; MailData += "\r\n"; // devider between message headers and message body MailData += "This is just a test!\r\n"; MailData += ".\r\n"; // end of message MailData += "QUIT\r\n"; Debug.Print(MailData);I can't test this part, but I suppose this should work:
smtp.Connect(new IPEndPoint(mailip, 25)); smtp.Send(Encoding.UTF8.GetBytes(MailData)); smtp.Close();
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#9
Posted 29 April 2011 - 02:57 PM
string MailData = ""; MailData += "HELO localhost\r\n"; MailData += "MAIL FROM: test@domain.com\r\n"; MailData += "RCPT TO: myemail@domain.com\r\n"; MailData += "DATA\r\n"; MailData += "Subject: Test Email\r\n"; MailData += "From: \"My Name\" <test@domain.com>\r\n"; MailData += "To: \"your name\" <myemail@domain.com\r\n"; MailData += "\r\n"; // devider between message headers and message body MailData += "This is just a test!\r\n"; MailData += ".\r\n"; // end of message MailData += "QUIT\r\n"; Debug.Print(MailData);I can't test this part, but I suppose this should work:smtp.Connect(new IPEndPoint(mailip, 25)); smtp.Send(Encoding.UTF8.GetBytes(MailData)); smtp.Close();
Ok, so this doesn't seem to work for me. Does anyone know of a way to read what the SMTP server is responding with each line? I'm not even sure if you can read and write on the same socket.
Thanks
#10
Posted 29 April 2011 - 06:06 PM
HELO localhost -- caused problems. I needed a real domain. I'm using google.com. The domain of the mail server caused issues as well.
I used Stefan's code post above and nothing happened. I had to put some Sleep's in the code because Netduino was sending commands faster than the mail server could respond. Here's what I ended up with. I hope it helps someone.
static IPAddress mailip = IPAddress.Parse("192.168.1.15"); Socket smtp = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string MailBody = ""; MailBody += "Subject: Test Email\r\n"; MailBody += "From: \"Netduino\" <netduino@domain.com>\r\n"; MailBody += "To: \"My Email\" <email@domain.com\r\n"; MailBody += "\r\n"; // devider between message headers and message body MailBody += "This is just a test!\r\n"; MailBody += ".\r\n"; Thread.Sleep(500); led.Write(true); Debug.Print("Connecting..."); smtp.Connect(new IPEndPoint(mailip, 25)); smtp.Send(Encoding.UTF8.GetBytes("HELO google.com\r\n")); Thread.Sleep(100); smtp.Send(Encoding.UTF8.GetBytes("MAIL FROM: netduino@domain.com\r\n")); Thread.Sleep(100); smtp.Send(Encoding.UTF8.GetBytes("RCPT TO: email@domain.com\r\n")); Thread.Sleep(100); smtp.Send(Encoding.UTF8.GetBytes("DATA\r\n")); Thread.Sleep(100); smtp.Send(Encoding.UTF8.GetBytes(MailBody)); Thread.Sleep(100); smtp.Send(Encoding.UTF8.GetBytes("QUIT\r\n")); smtp.Close(); Debug.Print("Sent!"); led.Write(false);
#11
Posted 29 April 2011 - 08:46 PM
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#12
Posted 30 April 2011 - 01:34 AM
#13
Posted 30 April 2011 - 01:34 PM
#14
Posted 01 May 2011 - 01:16 AM
jmmille, now you're this far. Try reading the socket after every Send data. It will return a code followed by a string.
I once wrote a very simple SMTP mailer in VB6, perhaps you can read that source; https://stefan.co/ba...console-mailer/
Here in lies the problem Stefan.... which I think is possibly similar to what we have been experiencing with the Banksky client.
Taking jmmille's code, which works nicely, I add a call to the following function, before each 'send' statement.
public static void readresponse() { if (smtp.Poll(10000000, SelectMode.SelectRead)) { a = smtp.Available; smtp.Receive(ReceiveBuffer, a, SocketFlags.None); Response = new String(Encoding.UTF8.GetChars(ReceiveBuffer), 0, a); Debug.Print(Response); } else { Debug.Print("Timeout! Bytes Available to read = " + a); } }
The added code works fine under emulation and shows the SMTP server responses, however running it on the Netduino Plus, the SOCKET.POLL times out , suggesting that the SMTP server is not sending anything.
Am I missing something here?
#15
Posted 16 May 2011 - 02:11 PM
Hi, it's been a while since I took a look at this topic. While playing with telnet on my mail server, I noticed I only get responses from the mail server when I send the Helo, mail from and rcpt to commands. The only other response comes after the message is sent, which said something like "Message scheduled for delivery."The added code works fine under emulation and shows the SMTP server responses, however running it on the Netduino Plus, the SOCKET.POLL times out , suggesting that the SMTP server is not sending anything.
Am I missing something here?
Do you happen to be running the function in between the other commands?
#16
Posted 25 May 2011 - 11:27 PM
#17
Posted 23 October 2011 - 10:39 AM
#18
Posted 23 October 2011 - 10:47 AM
Funny, I know that ISP! Goedemiddag en welkom op de Netduino-forumsThe Bansky code works fine if I use the SMTP server of my Internet provider XS4ALL.
However I want to use a public SMTP server, so I can use the Netduino everywhere. Gmail does not work as noticed. Does anyone have an idea?
I tried GMX (www.gmx.com) because TLS/SSL is not required, however it seems that it does not work?
The Netduino Plus won't get support for TLS/SSL soon, I'm afraid a (small) ssl implementation will never fit in the Plus. I'm going to look into gmx.com. I wrote my own SMTP library (http://netmftoolbox....NET.SMTP_Client) and perhaps I can adjust it.
-edit- Okay, I've looked into smtp authentication specs, it requires some work, but I can implement it. I already had a work item about it, http://netmftoolbox....om/workitem/475 which I'm going to extend this week. Thanks for providing a good example; the gmx service!
Luckely GMX also listens to port 587 since my ISP blocks all outgoing traffic to port 25 except of their own SMTP-server; I have Ziggo as ISP since they're the only ones who could offer me 120Mbps at home
Edited by Stefan, 23 October 2011 - 11:04 AM.
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#19
Posted 23 October 2011 - 01:55 PM
SMTP_Client Sender = new SMTP_Client("mail.gmx.com", 587, SMTP_Client.AuthenticationTypes.Login, "[yourusername]@gmx.com", "[yourpassword]");
There are yet two other authentication types that the class doesn't support yet. This is due to a bug I walked into (http://netmf.codeple...m/workitem/1321) I am going to delay the other authentication types. This one worked for me
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#20
Posted 29 January 2012 - 05:54 AM
SMTP_Client Sender = new SMTP_Client("mail.gmx.com", 587, SMTP_Client.AuthenticationTypes.Login, "[yourusername]@gmx.com", "[yourpassword]");
This solved my problem though:
SMTP_Client Sender = new SMTP_Client(new SimpleNETMFSocket("mail.gmx.com", 587), SMTP_Client.AuthenticationTypes.Login, "[yourusername]@gmx.com", "[yourpassword]");
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users