locked
Send Email using Azure function without using third party tool RRS feed

  • Question

  • Hello Techies,

    I am trying to send email using Azure function and I wrote code (as below) for it and it is working fine locally.

    But when I am running function on Azure Portal then error is occurring :

    Error : Code has some error : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at

    Status: 500 Internal Server Error 

    Code as below :

        public static class Function1
        {
            [FunctionName("SendNotification")]
            public static async Task<HttpResponseMessage> Run(
                [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req,
                ILogger log)
            {
                try
                {
                    string from = "FromEmailId"; (GmailId)
                    string to = "ToEmailId"; (GmailId)
    
                    PipelineRun pipelineRun = new PipelineRun();
                    MailAddress mailAddress = new MailAddress(to);
                    MailMessage message = new MailMessage(from, to);
                    message.From = new MailAddress(from);
                    message.To.Add(to);
                    message.IsBodyHtml = false;
                    message.Subject = "ADF Success Notification";
                    message.Body = "Azure Data Factory Email Notification" + " " +
                    System.Environment.NewLine + "Pipeline Name      : " + pipelineRun.PipelineName + " " +
                    System.Environment.NewLine + "Pipeline RunId     : " + pipelineRun.RunId + " " +
                    System.Environment.NewLine + "Pipeline Message   : " + pipelineRun.Message + " " +
                    System.Environment.NewLine + "Pipeline Status    : " + pipelineRun.Status + " " +
                    System.Environment.NewLine + "Pipeline Runs for  : " + pipelineRun.DurationInMs;
    
                    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                    smtpClient.Credentials = new System.Net.NetworkCredential("GmailId".Trim(), "myPwd");
                    smtpClient.EnableSsl = true;
    
                    smtpClient.Send(message);
    
                    Console.WriteLine("Mail sent successfully");
                }
                catch (Exception ex)
                {
                    throw new Exception($"Code has some error : {ex.Message}");
                }
                return req.CreateResponse(HttpStatusCode.OK,"Done");
            }
        }
    

    In ADF, I configured Azure Function Activity as below

    

     Here I tried "Post" Method also but it is giving me same error.

     Could you guys please help me to fix this?

     Thanks in Advance


    Ramandeep

    Tuesday, December 24, 2019 7:03 AM

Answers

All replies