How can I do this? I have this in my old project:
void setUpNetwork()
{
eth.Interface.Open();
//instructs the Mainboard to use this module for all network communication, and assigns the networking stack to this module.
//This function is only needed if more than one network module is being used simultaneously. If not, this function should not be used.
NetworkInterfaceExtension.AssignNetworkingStackTo(eth.Interface);
eth.Interface.NetworkInterface.EnableStaticIP(ipAddress, subnetMask, gatewayAdress);
}
setUpNetwork is executed in public void ProgramStarted() {âŚ}
Hi,
this code is an example how the Network can be initialized if your router uses DHCP
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using GHI.Premium.Net;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace Ethernet_DHCP_Spider
{
public partial class Program
{
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
ethernet_J11D.Interface.CableConnectivityChanged +=new GHI.Premium.Net.EthernetBuiltIn.CableConnectivityChangedEventHandler(Interface_CableConnectivityChanged);
ethernet_J11D.Interface.NetworkAddressChanged +=new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
ethernet_J11D.Interface.Open();
if (NetworkInterfaceExtension.AssignedNetworkInterface == null)
{
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface == null");
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
}
else
{
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface.IsActivated " + NetworkInterfaceExtension.AssignedNetworkInterface.IsActivated);
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface != null");
if (NetworkInterfaceExtension.AssignedNetworkInterface.NetworkInterface.NetworkInterfaceType != Microsoft.SPOT.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
{
Debug.Print("NetworkInterfaceExtension.AssignedNetworkInterface.NetworkInterface.NetworkInterfaceType != Microsoft.SPOT.Net.NetworkInformation.NetworkInterfaceType.Ethernet");
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
}
}
ethernet_J11D.Interface.NetworkInterface.EnableDhcp();
}
void Interface_NetworkAddressChanged(object sender, EventArgs e)
{
Debug.Print("Interface_NetworkAddressChanged");
Debug.Print("New address for the Network Interface ");
Debug.Print("Is DhCp enabled: " + ethernet_J11D.Interface.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_J11D.Interface.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + ethernet_J11D.Interface.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + ethernet_J11D.Interface.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + ethernet_J11D.Interface.NetworkInterface.SubnetMask);
Debug.Print("Default Gateway: " + ethernet_J11D.Interface.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + ethernet_J11D.Interface.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < ethernet_J11D.Interface.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + ethernet_J11D.Interface.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
}
void Interface_CableConnectivityChanged(object sender, GHI.Premium.Net.EthernetBuiltIn.CableConnectivityEventArgs e)
{
//throw new NotImplementedException();
}
}
}
Thank you very much RoSchmi. Itâs very helpful what you posted.
But now I donât get error but the e-mail is not sent: The debug window is:
Program Started
NetworkInterfaceExtension.AssignedNetworkInterface.IsActivated True
NetworkInterfaceExtension.AssignedNetworkInterface != null
Interface_NetworkAddressChanged
New address for the Network Interface
Is DhCp enabled: True
Is DynamicDnsEnabled enabled: False
NetworkInterfaceType 6
Network settings:
IP Address: 192.168.2.106
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.2.1
Number of DNS servers:2
DNS Server 0:192.168.2.1
DNS Server 1:192.168.2.1
The thread ââ (0x3) has exited with code 0 (0x0).
Button Pressed!
587 smpt.gmail.com
System.Net.IPHostEntry
80.156.86.78:587
The program starts. After I press send button, I see on the debug window:
System.Net.IPHostEntry
80.156.86.78:587
I have entered the debug.print in every if-clause in source.cs:
public class SmtpClient
{
private string _SmtpServerName = null;
private int _Port = 0;
private enum SmtpState
{
NotConnected,
DomainAccepting,
MailFromAccepting,
RecipientAccepting,
DataCommandAccepting,
MessageAccepting,
ConnectionClosing
};
SmtpState state;
public SmtpClient(string SmtpServerName, Int32 Port)
{
if (Port < 0)
throw new ArgumentOutOfRangeException();
_SmtpServerName = SmtpServerName;
_Port = Port;
Debug.Print(_Port.ToString());
Debug.Print(_SmtpServerName.ToString());
}
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 = Convert.ToBase64String(encoding.GetBytes(username));
password = Convert.ToBase64String(encoding.GetBytes(password));
}
state = SmtpState.NotConnected;
/* Connect to the Server*/
// Figure out the Server IP Address
IPHostEntry SmtpServerHostEntry = Dns.GetHostEntry(_SmtpServerName);
Debug.Print(SmtpServerHostEntry.ToString());
IPEndPoint SmtpServerEndPoint = new IPEndPoint(SmtpServerHostEntry.AddressList[0],
_Port);
Debug.Print(SmtpServerEndPoint.ToString());
// Establish the connection with SMTP Server
Socket SmtpConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
SmtpConnection.Connect(SmtpServerEndPoint);
String ResponseStr = null;
while (SmtpConnection.Poll(-1, SelectMode.SelectRead))
{
byte[] ReceiveBuffer = new byte[SmtpConnection.Available];
SmtpConnection.Receive(ReceiveBuffer, ReceiveBuffer.Length, SocketFlags.None);
// The first 3 bytes hold the message number which is enough for us.
ResponseStr = new String(Encoding.UTF8.GetChars(ReceiveBuffer), 0, 3);
int Response = Int16.Parse(ResponseStr);
switch (state)
{
case SmtpState.NotConnected:
if (Response == 220) // Domain service ready.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("HELO " +
from.Split(new char[] { '@ ' })[1] + "\r\n"));
if (authenticate && username != "" && password != "")
{
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes("AUTH LOGIN\r\n"));
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes(username + "\r\n"));
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes(password + "\r\n"));
Thread.Sleep(300);
}
state = SmtpState.DomainAccepting;
Debug.Print("Response==220");
}
break;
case SmtpState.DomainAccepting:
if (Response == 250) // auth successful (if authentication enabled).
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("MAIL FROM:<" + from +
">\r\n"));
state = SmtpState.MailFromAccepting;
Debug.Print("DomainAccepting: Response==250");
}
break;
case SmtpState.MailFromAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("RCPT TO:<" + recipient
+ ">\r\n"));
state = SmtpState.RecipientAccepting;
Debug.Print("MailFromAccepting: Response==250");
}
break;
case SmtpState.RecipientAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("DATA\r\n"));
state = SmtpState.DataCommandAccepting;
Debug.Print("RecipientAccepting: Response==250");
}
break;
case SmtpState.DataCommandAccepting:
if (Response == 354) //Start mail input; end with <CRLF>.<CRLF>.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("Subject: " + subject +
"\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes("From: " + from +
"\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes("To: " + recipient +
"\r\n\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes(body + "\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes(".\r\n"));
state = SmtpState.MessageAccepting;
Debug.Print("DataCommandAccepting: Response==354");
}
break;
case SmtpState.MessageAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("QUIT\r\n"));
state = SmtpState.ConnectionClosing;
Debug.Print("MessageAccepting: Response==250");
}
break;
case SmtpState.ConnectionClosing:
if (Response == 221) // Requested mail action okay, completed.
{
SmtpConnection.Close();
Debug.Print("ConnectionClosing: Response==221");
return;
}
break;
}
}
SmtpConnection.Close();
throw new Exception("SMTP connection Timeout");
}
}
587 = secure. You canât use SSL/TLS with this code. Since I only speak/read English, I canât tell for sure if thereâs a âuse only secureâ option on your screenshot, but you need to use a SMTP server that permits the use of unencrypted/unsecured SMTP transfer, usually to port 25.
Now I have created a new account in outlook which allows smtp with port 25.
My output window delivers on press on the button first:
Button Pressed!
25 smtp-mail.outlook.com
System.Net.IPHostEntry
65.55.96.11:25
Response==220
DomainAccepting: Response==250
After a few seconds I get #### Exception System.ArgumentNullException - 0x00000000 (5) ####.
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 = Convert.ToBase64String(encoding.GetBytes(username));
password = Convert.ToBase64String(encoding.GetBytes(password));
}
state = SmtpState.NotConnected;
/* Connect to the Server*/
// Figure out the Server IP Address
IPHostEntry SmtpServerHostEntry = Dns.GetHostEntry(_SmtpServerName);
Debug.Print(SmtpServerHostEntry.ToString());
IPEndPoint SmtpServerEndPoint = new IPEndPoint(SmtpServerHostEntry.AddressList[0],
_Port);
Debug.Print(SmtpServerEndPoint.ToString());
// Establish the connection with SMTP Server
Socket SmtpConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
SmtpConnection.Connect(SmtpServerEndPoint);
String ResponseStr = null;
while (SmtpConnection.Poll(-1, SelectMode.SelectRead))
{
byte[] ReceiveBuffer = new byte[SmtpConnection.Available];
SmtpConnection.Receive(ReceiveBuffer, ReceiveBuffer.Length, SocketFlags.None);
// The first 3 bytes hold the message number which is enough for us.
ResponseStr = new String(Encoding.UTF8.GetChars(ReceiveBuffer), 0, 3);
int Response = Int16.Parse(ResponseStr);
switch (state)
{
case SmtpState.NotConnected:
if (Response == 220) // Domain service ready.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("HELO " +
from.Split(new char[] { '@ ' })[1] + "\r\n"));
if (authenticate && username != "" && password != "")
{
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes("AUTH LOGIN\r\n"));
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes(username + "\r\n"));
Thread.Sleep(300);
SmtpConnection.Send(Encoding.UTF8.GetBytes(password + "\r\n"));
Thread.Sleep(300);
}
state = SmtpState.DomainAccepting;
Debug.Print("Response==220");
}
break;
case SmtpState.DomainAccepting:
if (Response == 250) // auth successful (if authentication enabled).
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("MAIL FROM:<" + from +
">\r\n"));
state = SmtpState.MailFromAccepting;
Debug.Print("DomainAccepting: Response==250");
}
break;
case SmtpState.MailFromAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("RCPT TO:<" + recipient
+ ">\r\n"));
state = SmtpState.RecipientAccepting;
Debug.Print("MailFromAccepting: Response==250");
}
break;
case SmtpState.RecipientAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("DATA\r\n"));
state = SmtpState.DataCommandAccepting;
Debug.Print("RecipientAccepting: Response==250");
}
break;
case SmtpState.DataCommandAccepting:
if (Response == 354) //Start mail input; end with <CRLF>.<CRLF>.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("Subject: " + subject +
"\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes("From: " + from +
"\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes("To: " + recipient +
"\r\n\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes(body + "\r\n"));
SmtpConnection.Send(Encoding.UTF8.GetBytes(".\r\n"));
state = SmtpState.MessageAccepting;
Debug.Print("DataCommandAccepting: Response==354");
}
break;
case SmtpState.MessageAccepting:
if (Response == 250) // Requested mail action okay, completed.
{
SmtpConnection.Send(Encoding.UTF8.GetBytes("QUIT\r\n"));
state = SmtpState.ConnectionClosing;
Debug.Print("MessageAccepting: Response==250");
}
break;
case SmtpState.ConnectionClosing:
if (Response == 221) // Requested mail action okay, completed.
{
SmtpConnection.Close();
Debug.Print("ConnectionClosing: Response==221");
return;
}
break;
}
}
SmtpConnection.Close();
throw new Exception("SMTP connection Timeout");
}
The exception is thrown in
Do you know what can be the reason??
My event_handler code is:
```cs
private void Button_PressEvent(object sender)
{
Debug.Print("Button Pressed!");
SmtpClient mySmtp;
int SMTP_PORT = 25;
string MAIL_SERVER = "smtp-mail.outlook.com";
string SENDER_ADDRESS = "hochschule_karlsruhe@ outlook.de";
string RECIPIENT_ADDRESS = "hochschule_karlsruhe@ outlook.de";
string SUBJECT = "Your Subject";
string BODY = "This is a test Message";
string SMTP_USERNAME = "hochschule_karlsruhe@ outlook.de";
string SMTP_PASSWORD = "test1234";
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);
Debug.Print("Send erreicht");
}
As Architect pointed out, the 530 in your response string is an error and your clue to the fault. Do a search on SMTP 530 and youâll see whatâs wrong.
@ SB -
if you can send E-mails with e.g. MS Office Outlook to the E-Mail server you could install Wireshark to analyse the network messages and to compare the differences between MS Outlook and your FEZ E-Mail Client.