OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Mail sending functionality through third party "MailTrap" works on local machine but not working on Azure

  • Thread starter Thread starter Ivan Ivanov
  • Start date Start date
I

Ivan Ivanov

Guest
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

Code:
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.
<p>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</p>
<pre><code>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;
}
}
}
}
</code></pre>
<p>) 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. (<a href="https://i.sstatic.net/2eIo98M6.jpg" rel="nofollow noreferrer">The failure shown in Azure</a>) (<a href="https://i.sstatic.net/Jf9LvYD2.jpg" rel="nofollow noreferrer">how it looks on the web page</a>)</p>
<p>I tried to send a message but the message doesn't go through.</p>
Continue reading...
 

Latest posts

Top