Good Morning,
I am working on a project that requires a low temperature alert when the temperature is below 50 degrees. In addition it needs to send the IP address to connect to. The solution is to send an email using GMX mail. http://www.gmx.com/. Once an email address is created the project below will send the email message to any email address. I am only using GMX for sending the message to a Gmail address. Pavel Bansky inspired parts of the project links to is blog and source code are below.
Sub Main()
'Set the properties
smtp.Properties.Host = "mail.gmx.com"
smtp.Properties.Port = 587
smtp.Properties.UserName = "YourEmailAddress@gmx.us"
smtp.Properties.UserPassword = "YourPasswrod"
smtp.Properties.SenderAddress = "YourEmailAddress@gmx.us"
smtp.Properties.RecipientAddress = "AnyOne@gmail.com"
smtp.Properties.Subject = "Netduino sent this message"
smtp.Properties.Body = "Hello world my IP address is "
'Send the message
Dim Response As String = smtp.Send
If Response <> "250" Then
Debug.Print(Response)
Else
Debug.Print("Success!")
Debug.Print("My IPaddress is: " & smtp.Properties.IPAddress)
End If
End Sub
'Put the code below in a new class object
Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Collections
Imports Microsoft.SPOT
Imports Microsoft.VisualBasic.Constants
'*****The work around in AUTH PLAIN and other functions were inspired by Pavel Bansky
'*****Dim Data() As Byte = Encoding.UTF8.GetBytes("\0" & Properties.UserName & "\0" & Properties.UserPassword)
'*****SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(Data))
'*****The lines above will not work see the links below
Public Class smtp
Public Structure Properties
Public Shared SenderAddress As String
Public Shared RecipientAddress As String
Public Shared UserName As String
Public Shared UserPassword As String
Public Shared Subject As String
Public Shared Body As String
Public Shared Port As Integer = 25
Public Shared Host As String
Public Shared IPAddress As String
End Structure
Private Shared Socket As Socket
Public Shared Function Send() As String
Dim Response As String = String.Empty
Dim ReturnCode As String = String.Empty
Try
Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Properties.Host)
' Create socket and connect to the server's IP address and port
Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'Connect
Socket.Connect(New IPEndPoint(hostEntry.AddressList(0), Properties.Port))
'Get the Mail Servers response
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "220" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("First Response should be 220: " & Response)
'******Authentication
SendCommand("EHLO localhost")
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "250" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("Authentication Response should be 250: " & Response)
'Get outside IP address
If InString(Response, "[") Then
Dim ipStart As Integer = Response.IndexOf(CChar("[")) + 1
Dim ipEnd As Integer = Response.IndexOf(CChar("]"))
Properties.IPAddress = Response.Substring(ipStart, ipEnd - ipStart)
End If
'Login
If InString(Response, "LOGIN") Then
SendCommand("AUTH LOGIN")
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "334" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("Authentication Response AUTH LOGIN should be 334: " & Response)
Dim data() As Byte = Encoding.UTF8.GetBytes(Properties.UserName)
SendCommand(System.Convert.ToBase64String(data))
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "334" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("Authentication Response User Name should be 334: " & Response)
data = Encoding.UTF8.GetBytes(Properties.UserPassword)
SendCommand(System.Convert.ToBase64String(data))
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "235" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("Authentication Response for User Password should be 235: " & Response)
Else
'Plain
If InString(Response, "PLAIN") Then
Dim userBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserName)
Dim userLen As Integer = userBytes.Length
Dim passBytes As Byte() = UTF8Encoding.UTF8.GetBytes(Properties.UserPassword)
Dim login As Byte() = New Byte(userLen + passBytes.Length + 1) {}
login(0) = 0
login(userLen) = 0
Array.Copy(userBytes, 0, login, 1, userLen)
Array.Copy(passBytes, 0, login, userLen + 2, passBytes.Length)
SendCommand("AUTH PLAIN " + System.Convert.ToBase64String(login))
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
If ReturnCode <> "235" Then
Socket.Close()
Socket = Nothing
Return Response
End If
Debug.Print("Authentication Response for AUTH PLAIN should be 235: " & Response)
End If
End If
SendCommand("MAIL From:<" + Properties.SenderAddress + ">")
Response = GetResponse()
Debug.Print("Mail from Response should be 250 " & Response)
'To
SendCommand("RCPT To:<" + Properties.RecipientAddress + ">")
Response = GetResponse()
Debug.Print("Mail To Response should be 250 or 251 " & Response)
'Make the message
Dim message As String = "Subject: " + Properties.Subject & vbCrLf
message += "From:<" & Properties.SenderAddress & ">" & vbCrLf
message += "To:<" & Properties.RecipientAddress & ">" & vbCrLf
Dim now As DateTime = DateTime.Now
message += "Date: " + now.ToString("ddd, d MMM yyyy HH:mm:ss " & vbCrLf)
message += "Content-Type: text/plain;" & vbCrLf & vbTab & "charset=""UTF-8""" & vbCrLf & vbCrLf
message += Properties.Body & Properties.IPAddress
'send the message
SendCommand("DATA") '
Response = GetResponse()
Debug.Print("Response to DATA should be 354 " & Response)
SendCommand(message & vbCrLf & "." & vbCrLf)
Response = GetResponse()
ReturnCode = Response.Substring(0, 3)
Debug.Print("Response to send the message should be 250 " & Response)
Socket.Close()
Socket = Nothing
Catch ex As Exception
Socket.Close()
Socket = Nothing
Debug.Print("Error in smtp Send: " & ex.ToString)
Return "Error in smtp Send: " & ex.ToString
End Try
Return ReturnCode
End Function
Private Shared Sub SendCommand(command As String)
If command.LastIndexOf(CChar(vbCrLf)) < 1 Then
command += vbCrLf
End If
Dim buffer As Byte() = Encoding.UTF8.GetBytes(command)
Socket.Send(buffer)
End Sub
Private Shared Function GetResponse() As String
Dim buffer As Byte() = New Byte(511) {}
Dim Response As String = String.Empty
While Socket.Poll(15000000, SelectMode.SelectRead)
Dim bytesRead As Integer = Socket.Receive(buffer)
Dim c As Char() = Encoding.UTF8.GetChars(buffer)
For i As Integer = 0 To c.Length - 1
Response += c(i)
Next
If Response.LastIndexOf(CChar(vbCrLf)) > 1 Then
Exit While
End If
End While
Return Response
End Function
Public Shared Function InString(ByVal String1 As String, ByVal StringToFind As String) As Boolean
Try
If String1 = String.Empty Then Return False
If StringToFind = String.Empty Then Return False
String1 = String1.ToUpper
StringToFind = StringToFind.ToUpper
If String1.IndexOf(StringToFind) = -1 Then
Return False
Else
Return True
End If
Catch
Return False
End Try
End Function
End Class