Main Site Documentation

Possible to send e-mail via button on Display T35?


#1

Hi,

is it possible to send e-mail via button on Display T35?


#2

yes.

go to codeshare area and do a search on SMTP


#3

The one does not depend on the other.
You can do anything an a button press, except making coffee (but I think there was a Gadgeteer Coffee Roster shortly :think:).

To send eMail, listen to @ Mike.


#4

I found the example in codeshare. But what does mean
Error 3 The name ‘ConvertBase64’ does not exist in the current context

The code in Source.cs:
public void Send(string from, string recipient, string subject, string body,
bool authenticate = false, string username = “”, string password = “”)
{
if (username != “” && password != “”)
{
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
username = ConvertBase64.ToBase64String(encoding.GetBytes(username));
password = ConvertBase64.ToBase64String(encoding.GetBytes(password));
}

Am I missing a package?


#5

There was a type in this code, change to following code:


System.Text.Encoding encoding = new System.Text.UTF8Encoding();
username = Convert.ToBase64String(encoding.GetBytes(username));
password = Convert.ToBase64String(encoding.GetBytes(password));


#6

I don’t understand what you mean…

I downloaded the example from:
https://www.ghielectronics.com/community/codeshare/entry/327

There are two cs files for sending e-mail. How can I use them to send the mail via a button_press_event?
GHIElectronics.NETMF.Glide.UI.Button buttonSend= (GHIElectronics.NETMF.Glide.UI.Button)window.GetChildByName(“Send”);
buttonSend.PressEvent += new OnPress(button_SendPressEvent);

private void button_SendPressEvent(object sender)
{ how to fill this??
}


#7

Ok sry,
I looked wrong. I’ll change the code, but my question regarding the use of the code example is still not answered :slight_smile:


#8

Take the Smtp client class into your project

put

SmtpClient mySmtp;

 mySmtp = new SmtpClient(MAIL_SERVER, SMTP_PORT);

// Usage for anonymous (unauthenticated) Send...default parameter values will be used for auth params
 mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY);

// Usage for authenticated Send - SMTP username and password will be base64 encoded by the
//   Send method.
 mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY, true, SMTP_USERNAME, SMTP_PASSWORD);

In your button handler and replace the ‘all caps’ words with your values.
This sniped I found in code share
https://www.ghielectronics.com/community/codeshare/entry/327
When clicking on the Usage link.


#9

In your Button OnPress Event, add the following code:



           SmtpClient mySmtp;

            int SMTP_PORT = 25;
            string MAIL_SERVER = "yourmailserveraddress";
            string SENDER_ADDRESS = "sendersemailaddress";
            string RECIPIENT_ADDRESS = "receipientsemailaddress";
            string SUBJECT = "Your Subject";
            string BODY = "This is a test Message";
            string SMTP_USERNAME = "youremailaddress";
            string SMTP_PASSWORD = "yourpassword";

            
            mySmtp = new SmtpClient(MAIL_SERVER, SMTP_PORT);

            // Usage for anonymous (unauthenticated) Send...default parameter values will be used for auth params
            //mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY);

            // Usage for authenticated Send - SMTP username and password will be base64 encoded by the
            //   Send method.
            mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY, true, SMTP_USERNAME, SMTP_PASSWORD);
        }




#10

@ scardinale - Isn’t that what I just wrote?


#11

Yes. We must have been posting simultaneously.


#12

@ scardinale - 1st 8)


#13

I tried it but got the following error:

Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (5)

#### Message: 
#### Microsoft.SPOT.Application::OnEvent [IP: 0090] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####

A first chance exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll
An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll

My code is:
SmtpClient mySmtp;
int SMTP_PORT = 25;
string MAIL_SERVER = “pop.gmail.com”;
string SENDER_ADDRESS = “SB@ gmail.com”;
string RECIPIENT_ADDRESS = “XY@ gmail.com”;
string SUBJECT = “Your Subject”;
string BODY = “This is a test Message”;
string SMTP_USERNAME = “SB@ gmail.com”;
string SMTP_PASSWORD = “SB”;
mySmtp = new SmtpClient(MAIL_SERVER, SMTP_PORT);
// Usage for anonymous (unauthenticated) Send…default parameter values will be used for auth params
//mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY);
// Usage for authenticated Send - SMTP username and password will be base64 encoded by the
// Send method.
mySmtp.Send(SENDER_ADDRESS, RECIPIENT_ADDRESS, SUBJECT, BODY, true, SMTP_USERNAME, SMTP_PASSWORD);

I’ m not sure whether MAIL_SERVER is correct. I found some information in the internet but didn’t know which one must be entered (s. attachment).
I used my original e-mail-adress with my correct password and recipient address was also my e-mail.

In file Source.cs I have seen:
public class SmtpClient
{
private string _SmtpServerName = null;
private int _Port = 0;

Is int_Port = 0 right?


#14

Where did the exception occur? Did you try to diagnose this yourself? In particular, it says null reference, have you checked what code that’s meant to be? Are you sure you have your events wired up correctly? Comment out the SMTP code and just do debug.print(“button pressed”) and make sure that works before worrying about other stuff.

And please, you need to know your SMTP mail setup before you go into this. If you don’t know you can get that to work, how will you know whether the problem is your code, or the settings? Use another SMTP program from a PC to make sure that works. GMail is a pretty bad choice to try to get working first time, do you have an email account on your local ISP for instance, they’re usually much more simple to get working.


#15

Now I have created a new project with only the display. (Ethernet is not added but plugged in FEZ)
I got the following error when I press on the button:

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (4)

#### Message: 
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### SmtpClient::Send [IP: 0048] ####
#### Button_Internet_Test.Program::Button_PressEvent [IP: 0045] ####
#### GHIElectronics.NETMF.Glide.UI.Button::TriggerPressEvent [IP: 000e] ####
#### GHIElectronics.NETMF.Glide.Display.DisplayObjectContainer::OnTouchDown [IP: 0035] ####
#### GHIElectronics.NETMF.Glide.Display.Window::TouchDownEvent [IP: 002e] ####
#### GHIElectronics.NETMF.Glide.TouchEventHandler::Invoke [IP: a0e0619a] ####
#### GHIElectronics.NETMF.Glide.GlideTouch::RaiseTouchDownEvent [IP: 0008] ####
#### GHIElectronics.NETMF.Glide.GlideTouch+TouchConnection::OnEvent [IP: 007f] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
Uncaught exception

What does mean socket exception?
I have replaced private int _Port = 0; by private int _Port = 80; Is that right?

The event_handler works because I see Debug.Print(“Button Pressed!”);

The error is thrown in this line in program.cs:
IPHostEntry SmtpServerHostEntry = Dns.GetHostEntry(_SmtpServerName);

I have the example from codeshare:
https://www.ghielectronics.com/community/codeshare/entry/327


#16

Which one should I take as
int SMTP_PORT = 587;
string MAIL_SERVER = “smtp.gmail.com”;
(s.attachment)

Is private int _Port = 80; in Source.cs 80 or 0??


#17

socket exception is a networking issue. You said you didn’t include networking, but you’re trying to use networking. So a socket exception is telling you that you forgot to set up the networking.

Have you tried stepping into the code at all? That would be a really good move. Find a point of interest, set a break-point and run until you hit that and step in and see what happens.

My guess, without reading any code, is that the source.cs file is simply a DEFAULT value that gets overridden by what you pass in.

You need to specify the correct port. Only you know that, because it is what your mail server requires.


#18

I’ve now looked at the source code in devhammer’s code. You can see the constructor overrides the port with whatever you use when you instantiate the class. So it doesn’t matter what you put in source.cs, it’ll be overwritten.

Also, his code doesn’t use ssl.


#19

Look at the ‘configure email client’ page from your provider (GMX) to get the correct port.
You need the standard (non SSL) port.
The Defined standard for SMTP is 25 (80 is HTTP)
But some providers use a different port (like 2525) to make it ‘harder’ for hackers ::slight_smile:

I think the line that initialized the port with ‘0’ is ok, because it’s overwritten in the constructor by the provided parameter I think.

To find your NullReference exception you should step through the code to find the line where it happens.
Then start over, go to the line where it happened and stop there. Then check every variable, … if it’s null.


#20

NullReference exception appears only in my old project with other modules and functions.
I have created a new project with only the display and the button on the display. When I press the button I see on output window “Button Pressed!”. Then the code with smtp is executed. I set the breakpoint in line 47 source.cs:
IPHostEntry SmtpServerHostEntry = Dns.GetHostEntry(_SmtpServerName);
Until here there is no exception. When I press F11 (step into) the following error happens:

Step into: Stepping over non-user code 'System.Net.Dns.GetHostEntry’
after few seconds:
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (4) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### SmtpClient::Send [IP: 0048] ####
#### Button_Internet_Test.Program::Button_PressEvent [IP: 0045] ####
#### GHIElectronics.NETMF.Glide.UI.Button::TriggerPressEvent [IP: 000e] ####
#### GHIElectronics.NETMF.Glide.Display.DisplayObjectContainer::OnTouchDown [IP: 0035] ####
#### GHIElectronics.NETMF.Glide.Display.Window::TouchDownEvent [IP: 002e] ####
#### GHIElectronics.NETMF.Glide.TouchEventHandler::Invoke [IP: a0e0619a] ####
#### GHIElectronics.NETMF.Glide.GlideTouch::RaiseTouchDownEvent [IP: 0008] ####
#### GHIElectronics.NETMF.Glide.GlideTouch+TouchConnection::OnEvent [IP: 007f] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

Since I need networking I plugged in the Ethernet J11 D. Is that right??
What can cause the socket exception??