我需要在不使用 sendgrid 和 office365 的情况下从 Azure Function 应用程序发送电子邮件。
通过使用 Outlook 和 Gmail smtp,该功能给出错误
using System;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
public static void Run(Stream myBlob, string name, ILogger log, ExecutionContext context)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
string fromEmail = System.Environment.GetEnvironmentVariable("SmtpUser");
string toEmail = "abc@gmail.com";
int smtpPort = Int32.Parse(System.Environment.GetEnvironmentVariable("SmtpPort"));
bool smtpEnableSsl = true;
string smtpHost = System.Environment.GetEnvironmentVariable("SmtpHost"); // your smtp host
string smtpUser = System.Environment.GetEnvironmentVariable("SmtpUser"); // your smtp user
string smtpPass = System.Environment.GetEnvironmentVariable("SmtpPassword"); // your smtp password
string subject = System.Environment.GetEnvironmentVariable("EmailSubject");
string message = "Hello, you are recieving message from Azure Function /n Thanks, /n Pooja";
log.LogInformation($" Pass : {smtpPort} : Email: {fromEmail} : Host: {smtpHost} : Subject : {subject}");
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Port = smtpPort;
client.EnableSsl = smtpEnableSsl;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = smtpHost;
client.Credentials = new System.Net.NetworkCredential(smtpUser, smtpPass);
MailMessage mail = new MailMessage();
mail.Subject = subject;
mail.From = new MailAddress(fromEmail);
mail.Body=message;
mail.To.Add(new MailAddress(toEmail));
try{
client.Send(mail);
log.LogInformation("Email sent");
}
catch(Exception ex){
log.LogInformation(ex.ToString());
}
}
System.Net.Mail.SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。