Reputation: 1
I'm new in development and this is basically my first project. So I created a simple web page with a form on it that sends an email to the owner. When I enter the data and click Send button, you can see on one of the pictures that in the console there is an Error. When I investigated further within Azure Portal in Failures it shows that every time I try to send an email the program throws an ArgumentNullException(see another screenshot). The thing is when I try this on my local pc the method
using MailKit.Net.Smtp;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MimeKit;
using TheHandymanOfCapeCod.Core.Configuration;
using TheHandymanOfCapeCod.Core.Contracts;
using TheHandymanOfCapeCod.Core.Models.MailService;
namespace TheHandymanOfCapeCod.Core.Services
{
public class MailService : IMailService
{
private readonly MailSettings _mailSettings;
private readonly ILogger logger;
public MailService(
IOptions<MailSettings> mailSettingsOptions,
ILogger<MailService> _logger)
{
_mailSettings = mailSettingsOptions.Value;
logger = _logger;
}
public async Task<bool> SendMailAsync(MailData mailData)
{
try
{
using (MimeMessage emailMessage = new MimeMessage())
{
MailboxAddress emailFrom = new MailboxAddress(_mailSettings.SenderName, _mailSettings.SenderEmail);
emailMessage.From.Add(emailFrom);
MailboxAddress emailTo = new MailboxAddress(mailData.EmailToName, mailData.EmailToId);
emailMessage.To.Add(emailTo);
// you can add the CCs and BCCs here.
//emailMessage.Cc.Add(new MailboxAddress("Cc Receiver", "[email protected]"));
//emailMessage.Bcc.Add(new MailboxAddress("Bcc Receiver", "[email protected]"));
emailMessage.Subject = mailData.EmailSubject;
BodyBuilder emailBodyBuilder = new BodyBuilder();
emailBodyBuilder.TextBody = mailData.EmailBody;
emailMessage.Body = emailBodyBuilder.ToMessageBody();
//this is the SmtpClient from the Mailkit.Net.Smtp namespace, not the System.Net.Mail one
using (SmtpClient mailClient = new SmtpClient())
{
await mailClient.ConnectAsync(_mailSettings.Server, _mailSettings.Port, MailKit.Security.SecureSocketOptions.StartTls);
await mailClient.AuthenticateAsync(_mailSettings.UserName, _mailSettings.Password);
await mailClient.SendAsync(emailMessage);
await mailClient.DisconnectAsync(true);
}
}
return true;
}
catch (Exception ex)
{
logger.LogError(ex, "Something went wrong!");
return false;
}
}
}
}
) works fine and I don't know how to debug when it's on the server. Obviously something does't work right. It says that parameter address in MailboxAddress.ctor is null. (The failure shown in Azure) (how it looks on the web page)
I tried to send a message but the message doesn't go through.
Upvotes: 0
Views: 127
Reputation: 1
I figured it out. The problem was that MailSettings was located in UserSecrets. While it wasn't a problem for VS to figure that out, for Azure it wasn't possible cause it's a secret I guess... So I moved MailSettings into appsettings.json, Published(Republished) and voila! Email sending works.
Upvotes: 0