How to send an email in lua?

Hello everyone,

I am sending an email through my app but the error happens.

Error Message:

Mail send failed                  timeout 

Here is my code:

local smtp = require 'socket.smtp' local emailaddress = "me@fazilmir.com" function sendMessage(subject, body)     local msg = {         headers = {             to = emailaddress,             subject = subject         },         body = body     }     local ok, err = smtp.send {         from = emailaddress,         rcpt = emailaddress,         source = smtp.message(msg),         user = emailaddress,         password = '\*\*\*\*\*\*\*\*',         server = 'fazilmir.com',         port = 25     }     if not ok then         print("Mail send failed", err)     end end function sendMailFunc() if isFormValidated then sendMessage(txtName.text.. " Sent A Query", "Some Message") end return true end

Server and port number are also correct because I already have used these settings with outlook.

Please any help would be highly appreciated.

I am also attaching a screenshot.

Thanks,

I did it with network.request. 

Sharing my code. I hope it will help someone.

LUA FILE

local alert local emailParams = {} local function emailNetworkListener( event )         if ( event.isError ) then             alert = native.showAlert ( "Error Occurred", "Some error has occurred", {"Ok"})           else                         if(string.find(event.response, "OK")) then                 txtName.text = "";                 txtPhone.text = "";                 txtEmail.text = "";                 txtMessage.text = "";                                 alert = native.showAlert ( "Mail Sent", "Your Query Has Been Sent", {"Ok"})                         else                        alert = native.showAlert ( "Error Occurred", "Something Went Wrong. Try Again!", {"Ok"})               end         end return true end function sendMailFunc() emailParams.body = "name="..txtName.text.."&phone="..txtPhone.text.."&email=" .. txtEmail.text .. "&message="..txtMessage.text.."&securityValue=\*\*\*\*\*\*\*\*\*\*\*\*\*" network.request("http://mywebsite.com/sendEmail.aspx", "POST", emailNetworkListener, emailParams)           return true end

ASPX FILE

//On Page Load NameValueCollection collectData = Request.Form; if (!string.IsNullOrEmpty(collectData["securityValue"]) && collectData["securityValue"] == "\*\*\*\*\*\*") { //Here you can get all the values string name = collectData["name"]; string phone = collectData["phone"]; //sendEmail is a user defined function. string msg = ""; msg = sendEmail(collectData["email"], name, phone); if (msg == "OK") Response.Write("OK"); else Response.Write("ERROR"); }

That’s how i achieved this task.

Happy Coding…  :slight_smile:

I did it with network.request. 

Sharing my code. I hope it will help someone.

LUA FILE

local alert local emailParams = {} local function emailNetworkListener( event )         if ( event.isError ) then             alert = native.showAlert ( "Error Occurred", "Some error has occurred", {"Ok"})           else                         if(string.find(event.response, "OK")) then                 txtName.text = "";                 txtPhone.text = "";                 txtEmail.text = "";                 txtMessage.text = "";                                 alert = native.showAlert ( "Mail Sent", "Your Query Has Been Sent", {"Ok"})                         else                        alert = native.showAlert ( "Error Occurred", "Something Went Wrong. Try Again!", {"Ok"})               end         end return true end function sendMailFunc() emailParams.body = "name="..txtName.text.."&phone="..txtPhone.text.."&email=" .. txtEmail.text .. "&message="..txtMessage.text.."&securityValue=\*\*\*\*\*\*\*\*\*\*\*\*\*" network.request("http://mywebsite.com/sendEmail.aspx", "POST", emailNetworkListener, emailParams)           return true end

ASPX FILE

//On Page Load NameValueCollection collectData = Request.Form; if (!string.IsNullOrEmpty(collectData["securityValue"]) && collectData["securityValue"] == "\*\*\*\*\*\*") { //Here you can get all the values string name = collectData["name"]; string phone = collectData["phone"]; //sendEmail is a user defined function. string msg = ""; msg = sendEmail(collectData["email"], name, phone); if (msg == "OK") Response.Write("OK"); else Response.Write("ERROR"); }

That’s how i achieved this task.

Happy Coding…  :slight_smile: