none
Email Sending using On-Premise SMTP Server/Gmail SMTP Server RRS feed

  • Question

  • Hi,

    We have created an Azure function to insert the rows in azure SQL database on scheduled basis. After inserting the row in sql database , we have written the code in azure function for sending an email using on-primise / gmail SMTP server. But we have received the below error.

    [Error] Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. System: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at.

    We have used the below code inside the azure function:

    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    SmtpServer.UseDefaultCredentials = false;
    mail.From = new MailAddress("XXX@gmail.com");
    mail.To.Add("xxxx@gmail.com");
    mail.Subject = "SQL database Rows Inserted - Using Azure Function";
    mail.Body = "Hi, A new row has been inserted in Azure SQL Database.";
    SmtpServer.Port = 25;
    SmtpServer.Credentials = new System.Net.NetworkCredential("XXXX@gmail.com", "XXXXXX");
    SmtpServer.EnableSsl = true;
    SmtpServer.Send(mail);

    Please help us to resolve this issue.

    Thank you in advance.



    • Edited by SIERRAODC Friday, March 23, 2018 7:04 AM
    Friday, March 23, 2018 6:41 AM

All replies

  • Hi SIERRAODC,
    Gmail requires you to use port 465 when SSL is enabled. Please try changing your SMTP port to the following line:

    SmtpServer.Port = 465;

    Let me know your results after you've made this change.

    Best Regards,

    Robert

    Friday, March 23, 2018 6:38 PM
  • Hi,

    Thank you for the feedback.

    We have used port 465 and we have received the below error:

    2018-03-26T14:26:49.936 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: One or more errors occurred. System: Failure sending mail. System: Unable to read data from the transport connection: net_io_connectionclosed.

    Thank you.

    Monday, March 26, 2018 2:29 PM
  • Connection to on-premise SMTP server is not supported by default in Azure Functions. Some recommended options are:

    1. Use the supported Sendgrid output binding to send emails, see https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-sendgrid
    2. Run your Azure Functions on an ILB ASE, see. https://github.com/Azure/Azure-Functions/wiki/Using-Azure-Functions-on-an-ILB-ASE

    Monday, March 26, 2018 5:00 PM
  • From my research, to use Gmail as a relay server, you need to use port 587. Try:

    SmtpClient SmtpServer = new SmtpClient{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    UseDefaultCredentials = false,
    Credentials = new System.Net.NetworkCredential("XXXX@gmail.com", "XXXXXX")
    };

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("XXX@gmail.com");
    mail.To.Add("xxxx@gmail.com");
    mail.Subject = "SQL database Rows Inserted - Using Azure Function";
    mail.Body = "Hi, A new row has been inserted in Azure SQL Database.";

    SmtpServer.Send(mail);
    Tuesday, March 27, 2018 9:38 PM
  • Hi,

    We have tried SendGrid ...i am able to send emails from azure function.

    Thank you.


    Nandhakumar R

    Thursday, March 29, 2018 10:13 AM
  • Hi,

    We have tried port like 25, 587 and 465..Not able to send emails from Azure functions.

    But If I use SendGrid method I can able to send emails from azure functions.

    Thank you.


    Nandhakumar R


    • Edited by SIERRATEC Thursday, March 29, 2018 10:15 AM
    Thursday, March 29, 2018 10:14 AM