Helper - SMTP Email Class for Gadgeteer-based Projects

[title]SMTP Email Class for Gadgeteer-based Projects[/title]
SMTP.cs is a class based on the code for the FEZ email-based door monitor on the wiki:

the code has been updated to support the ethernet module that ships with the FEZ Spider Starter Kit, as well as to support SMTP authentication. The class will automatically base64 encode the username and password, if provided, as required by SMTP.

I needed an email client for my gadgeteer project, and since I’m probably not the only one, I figured I’d share it.

@ devhammer - Hi! I probed your code, but I don’t know why doesn’t work…The authentication is unsatisfactory (this is my obtened results…)…what is the best server for this code? I’m used ‘gmail’.
Thank you

I have not tried the code against gmail, as I do not have a gmail account. If they support SMTP, and you’re sure that you’re connecting to the correct SMTP address, then you may need to double-check your credentials (username/password). You might also check any available FAQ on the gmail site for how to connect to gmail via SMTP.

Hope that helps.

gmail requires certifications…and a secure connection the use of HTTPS://

which has not been proven to work reliably… at least that’s what i’ve learned when i tried to do SMTP against Gmail

I use this free service:

(My smtp requirements are pretty small) but it works great on my NAS which notifies me of updates and disk issues/space warnings.

It also supports SSL connections too.


@ devhamm; Could you please tell me what kind of server you have used ?
@ aficionate: Do you tryed to make some modifications to send mail from gmail ? Do you succeed to send e-mail from gmail using spider ?

Thank you in advance.

Welcome to the forums!

It’s been a while since I wrote the code, but I tested it against my personal domain email account, which is a hosted SMTP server implementation. The code should work with any valid SMTP server.


Thank you for your reply.
Below you find the error message what I get:

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### SmtpClient::Send [IP: 0047] ####
#### HelloWebServer.Program::Identification_WebEventReceived [IP: 0046] ####
#### Gadgeteer.Networking.WebEvent::OnWebEventReceived [IP: 0050] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060

Une exception de première chance de type ‘System.Net.Sockets.SocketException’ s’est produite dans Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
Error invoking method “Gadgeteer.Networking.WebEvent” (check arguments to Program.BeginInvoke are correct)

As I began to develop with. NET I did some research in msdn to understand the line of code. But I can’t make relation between this error and the code.

I put these lines in Identification_WebEventReceived() function:

SmtpClient mySmtp;

mySmtp = new SmtpClient(“Free email accounts | Register today at”, 80);

mySmtp.Send(“alarmer@”, “test@”, “Hello”, “This is a test”, true, “alarme”, “password”);

Could you please help me to send this e-mail ?

Using debugger, It seems that the source of problem comes from this line:
IPHostEntry SmtpServerHostEntry = Dns.GetHostEntry(_SmtpServerName);

I changed the _SmtpServerName by and the error message is disappeared.
I changed the port number also to 80.
The problem now is that the program still blocked in this line:

while (SmtpConnection.Poll(-1, SelectMode.SelectRead)

Waiting your feedback I continue my investigation…

@ Mo7a - See @ JayJay’s response above…according to his response, Gmail requires an HTTPS connection. As I noted earlier in the thread, I’ve not used this code against Gmail, so I can’t really help there…sorry.

you can not do smtp to a web server. the web address in the SMPT constructor is a URL, not an IP address. further, port 80 is incorrect.

Mike is correct, sorry I did not notice that the format of your mail server address was incorrect.

In order to make this work, you need to have access to an SMTP server for which you have a valid account. SMTP uses port 25 by default (though some mail providers, including mine, change that to reduce intrusion attempts by bots).

So if your mail server was “” and your username was “foo” and your password was “bar” here’s what the code would look like:

SmtpClient mySmtp;
mySmtp = new SmtpClient(", 25);
// Usage for anonymous (unauthenticated) Send
// ...default parameter values will be used for auth params
// Usage for authenticated Send - SMTP username and password 
// will be base64 encoded by the Send method.

For the code to work, you would need to replace the placeholders above (all caps) with valid values, and also substitute your actual mail server address, username, and password.

I tryed with :
mySmtp = new SmtpClient(“”, 587);

=> Connection to server is succesful :slight_smile: Also the authentifiaction is accepted as the return was “250”

The problem is that when I try to send:
SmtpConnection.Send(Encoding.UTF8.GetBytes(“MAIL FROM:<” + from + “>\r\n”));

Requested mail action is NOK. I received “530”

“from” is an availbale adresse e-mail !

“MAIL FROM” is a variable which should be present in html of mail server ?

Thank you in advance for your reply :slight_smile:

Mo7a -

  1. What “from” email address are you using? (this is the RECIPIENT_ADDRESS variable in the example below). From the error it would seem that this source of the problem.

  2. Is there a particular reason why you are not using the mySmtp object to send the email?

// Usage for authenticated Send - SMTP username and password 
// will be base64 encoded by the Send method.

Or is the information (530) you provided when you step into the code?

At this point in time, you should step back and confirm you can do a standard SMTP send using your credentials and a standard SMTP mail package. Personally, I like to use the BLAT command line (see ). If you prove that you can connect on a specific port to a specific server with specific credentials, and send an email from a specific address to another specific address, this gives you the information you need to move over to the netmf world and send the same email using the same credentials etc. 99% of the time an issue like you’ve mentioned is more likely to be related to the things that netmf assumes will work (like you know your mail server name, you know the port, you have permission etc) and diagnosing this will likely be easier outside netmf.

1/ The first argument of mySmtp.Send is SENDER_ADDRESS and this one is the “from” adress. Any way I’m using the same e-mail adresse (my personel gmail adresse) for SENDER_ADDRESS and RECIPIENT_ADDRESS

2/ I’m using the same function. In debug mode, in line :
case SmtpState.MailFromAccepting:
if (Response == 250) // Requested mail action okay, completed.
the Response value is equal to “530”. So I conclude that this is a negative response from server…

530 is “Authentication required”.

Through my work we occasionally have to set up digital photocopiers to scan to G-Mail. To do this we have to use both SSL (on port 465) and SMTP Authentication for it to work.

See the link below regarding G-Mail SMTP Settings:

I am eager to back to home and try all this good things :slight_smile:
Many thanks


I can confirm the smtp code is working on my hydra board and the german provider strato using authenticate on port 25.

SmtpClient mySmtp = new SmtpClient(“”,25);

Here is my logging response from the Strato SMTP Server:

Thread Send Mail
Mail Server Response:220
Mail Server Response:250
Mail Server Response:334
Mail Server Response:334
Mail Server Response:235
Mail Server Response:250
MailFrom Accepting
Mail Server Response:250
MailRecipient Accepting
Mail Server Response:354
MailDataCommand Accepting
Mail Server Response:250
Mail Message Accepting
Mail Server Response:221