Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

SMTP with .NET Micro Framework


  • Please log in to reply
30 replies to this topic

#1 mpearson

mpearson

    Member

  • Members
  • PipPip
  • 17 posts

Posted 26 April 2011 - 04:47 PM

Hello,
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 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 26 April 2011 - 07:04 PM

Hi,

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


"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 andysa

andysa

    New Member

  • Members
  • Pip
  • 9 posts

Posted 27 April 2011 - 12:53 AM

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. Edit: I have since read elsewhere in these forums that socket connection timeouts do not generate exceptions and code execution continues from the next statement. With that in mind, I now believe that the problem is a failure to obtain a socket. This is the code responsible for obtaining a socket, within the Bansky client: // Get server's IP address. IPHostEntry hostEntry = Dns.GetHostEntry(this.Host); Debug.Print(" Resolved IP address of " + hostEntry.AddressList[0]); // Create socket and connect to the server's IP address and port socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(new IPEndPoint(hostEntry.AddressList[0], this.Port)); The code is correctly resolving the IP address of the SMTP server. this.host and this.port hold the SMTP address and port 25 respectively Can anybody see anything obvious here, that would allow this to work under emulation, but not when running in the target?

#4 jmmille

jmmille

    New Member

  • Members
  • Pip
  • 5 posts

Posted 27 April 2011 - 01:18 PM

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+?

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.

#5 mpearson

mpearson

    Member

  • Members
  • PipPip
  • 17 posts

Posted 27 April 2011 - 04:31 PM

Yes, I was experiencing the same issues except I knew less about what was going on. I would love to know if there is a solution as well.

#6 mpearson

mpearson

    Member

  • Members
  • PipPip
  • 17 posts

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 jmmille

jmmille

    New Member

  • Members
  • Pip
  • 5 posts

Posted 29 April 2011 - 12:07 PM

Let us know if you get something working. I can find another route that does not require authentication.

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.

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 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 29 April 2011 - 01:04 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.

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();

jmmille, As stated before I don't have a netduino plus, but I have some basic understandings of internet protocols ;)

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();

"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#9 jmmille

jmmille

    New Member

  • Members
  • Pip
  • 5 posts

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 jmmille

jmmille

    New Member

  • Members
  • Pip
  • 5 posts

Posted 29 April 2011 - 06:06 PM

Ok, with a lot of help from Stefan, this appears to be working without authentication.

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 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 29 April 2011 - 08:46 PM

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/
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#12 mpearson

mpearson

    Member

  • Members
  • PipPip
  • 17 posts

Posted 30 April 2011 - 01:34 AM

jmmille and Stefan, Thank you very much for your help! This is a great community. Being new to .net programming, this type of help is much appreciated. Regards, Mike

#13 andysa

andysa

    New Member

  • Members
  • Pip
  • 9 posts

Posted 30 April 2011 - 01:34 PM

Agreed.... well done guys. That bit of code works for me also. However, I am still curious what is preventing the Bansky client from working, which has similar socket creation methods. I am wondering if it also, suffers from some kind of timing issues?

#14 andysa

andysa

    New Member

  • Members
  • Pip
  • 9 posts

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 jmmille

jmmille

    New Member

  • Members
  • Pip
  • 5 posts

Posted 16 May 2011 - 02:11 PM

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?

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."

Do you happen to be running the function in between the other commands?

#16 Tekati

Tekati

    New Member

  • Members
  • Pip
  • 1 posts

Posted 25 May 2011 - 11:27 PM

I have recompiled the Bansky code under Visual Studio 2010 and have it working on a Netduino just fine with authentication. Works both for the Emulator and also when I push it to the actual Netduino. What part was not working for you guys?

#17 kameleon

kameleon

    New Member

  • Members
  • Pip
  • 1 posts

Posted 23 October 2011 - 10:39 AM

The 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?

#18 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 23 October 2011 - 10:47 AM

The 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?

Funny, I know that ISP! Goedemiddag en welkom op de Netduino-forums :D

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.

"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#19 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 23 October 2011 - 01:55 PM

Works now with:
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 :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#20 bmccutch

bmccutch

    New Member

  • Members
  • Pip
  • 5 posts

Posted 29 January 2012 - 05:54 AM

Thanks Stefan works great with GMX, although the following code did not work for me:
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]");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.