0

I have added code which will send emails to the receiving party. As of now I need to add the same email address in "from" email and "username"(config email) to send the email else it will fail. But I wish to have different "from" email and not asking users for password and using one config mail to send email so multiple users can consume this service to send emails. Is it possible to do so?

Here is my code:

public async Task<string> Send(string from, string to, string subject, string html, string userName, string password)
{
    // create message
    var email = new MimeMessage();
    email.From.Add(MailboxAddress.Parse(from));
    email.To.Add(MailboxAddress.Parse(to));
    email.Subject = subject;
    email.Body = new TextPart(TextFormat.Html) { Text = html };
    using var smtp = new SmtpClient();
    smtp.Connect("smtp.live.com", 587, SecureSocketOptions.StartTls);
    smtp.Authenticate(userName, password);
    smtp.Send(email);
    smtp.Disconnect(true);
    return "Email Set";
}
Himanshu
  • 31,810
  • 31
  • 111
  • 133

1 Answers1

2

Yes, but you'll need to use a private SMTP server that will accept different addresses in the From: header and the authenticate commands. None of the widely-used public SMTP servers allow this as far as I know.

jstedfast
  • 35,744
  • 5
  • 97
  • 110