【发布时间】:2019-08-02 13:05:56
【问题描述】:
我需要在不使用 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 Authentication Required。
【问题讨论】:
-
这听起来很相关:stackoverflow.com/a/707892/1537195 看看那个问题和接受的答案,看看这是否对你有帮助。
标签: azure azure-functions azure-blob-storage azure-function-app