1

When I try to send email to customers, I'm sorry to send an email to them.

i have look here:

The error is here:

client.Send(mail);

The way I've built the code is like this:

string title = nameString;

var viewModel = new EmailModel
{
    getUrl = m.RemoveLinkUrl(),
    Title = title,
    FullName = item.Navn,
    text = text.ToHtmlString()
};

var resultMail = await _viewRenderService.RenderToStringAsync("~/Views/Templates/newMail.cshtml", viewModel);

MailMessage mail = new MailMessage(m.mailFrom(), item.Brugernavn);
SmtpClient client = new SmtpClient
{
    Port = m.port(),
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Host = m.host()
};
mail.Subject = title;
mail.Body = Regex.Replace(resultMail, "<[^>]*>", "");
client.Send(mail);<--- Error here

error are :

SmtpException: Mailbox unavailable. The server response was: Unauthenticated senders not allowed

  • Have you set the client.Credentials = new System.Net.NetworkCredential("user@domain.com","password"); in code or somewhere else in .config? – Martheen Apr 01 '18 at 15:27
  • The message is pretty clear, you are not passing credentials to the smtp server... – Shane Ray Apr 01 '18 at 15:27

2 Answers2

1

I have designed a component that handles Emails

First add my class to your project.

using System.ComponentModel;
using System.Net.Mail;
using System.Net;
using System.ComponentModel.DataAnnotations;

namespace Hector.Framework.Controls
{
    public class MailMessageControl : Component
    {
        private MailMessage Mail = new MailMessage();
        private SmtpClient SmtpClient = new SmtpClient();

        public MailMessageControl()
        {
            Host = "smtp.gmail.com";
            Port = 587;
            EnableSSL = true;
        }

        public string Host
        {
            get => SmtpClient.Host;
            set => SmtpClient.Host = value;
        }

        public int Port
        {
            get => SmtpClient.Port;
            set => SmtpClient.Port = value;
        }

        public bool EnableSSL
        {
            get => SmtpClient.EnableSsl;
            set => SmtpClient.EnableSsl = value;
        }

        public void AttachFile(string path)
        {
            Mail.Attachments.Add(new Attachment(path));
        }

        public void SetCredentials(string mail, string password)
        {
            SmtpClient.Credentials = new NetworkCredential(mail, password);
        }

        public void SetSender(string mail)
        {
            Mail.From = new MailAddress(mail);
        }

        public void AddAddressSee(string mail)
        {
            Mail.To.Add(mail);
        }

        public void SetSubject(string subject)
        {
            Mail.Subject = subject;
        }

        public void SetBody(string body, bool isHTML)
        {
            Mail.IsBodyHtml = isHTML;
            Mail.Body = body;
        }

        public bool SendEmail()
        {
            try
            {
                SmtpClient.Send(Mail);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool IsValidEmail(string email)
        {
            try
            {
                return new MailAddress(email).Address == email;
            }
            catch
            {
                return false;
            }
        }

        public bool EmailIsValidated(string email)
        {
            return new EmailAddressAttribute().IsValid(email);
        }
    }
}

Usage example:

Hector.Framework.Controls.MailMessageControl mail = new Hector.Framework.Controls.MailMessageControl();
mail.SetCredentials("Your gmail email", "Your gmail password");
mail.SetSender("Sender mail");
mail.AttachFile("Your file path"); //If you want send file
mail.AddAddressSee("Add mail to receive your message");
mail.SetSubject("Subject");
mail.SetBody("Body", false);

if(mail.SendEmail())
{
  //Mail send correctly
}
else
{
  //Error
}

Now go to the following link: https://myaccount.google.com/lesssecureapps

And enable this switch:

Screenshot This allows your program to use your credentials to send emails, if you do not activate it, it may result in an error.

After activating it, try to send a message

Héctor M.
  • 2,302
  • 4
  • 17
  • 35
0

Your code is 'ok'-ish for anonymous sending but your email server, you try to send, wants also that you add the 'send' credentials.

Normally you would keep the SmtpClient configuration in .config file of your application and within the code would do only

SmtpClient client = new SmtpClient();

The line above will read the configutration from the application config file - giving it fully under user configuration. Also giving him the option to enter the SMTP credentials.

here is the sample to send via Gmail:

<configuration>
<system.net>
    <mailSettings>
        <smtp from="Mail Displayname &lt;xxxx.yyy@gmail.com&gt;" deliveryMethod="Network">
            <network host="smtp.gmail.com" enableSsl="true" port="587" password="password" userName="xxxx.yyy@gmail.com"></network>
        </smtp>
    </mailSettings>
</system.net>
</configuration>
the
  • 1
  • 2