0

Send an email with your domain credential of outlook: With the following details:

POP Settings Server: outlook.office365.com Port: 995 Encryption: SSL/TLS

SMTP Settings Server: smtp.office365.com Port: 587 Encryption: STARTTLS

How to create an email service with the exchange in .net core.

MailMessage mailMessage = new MailMessage(_options.Value.Mailbox, receiverEmail, subject, emailBody);

            mailMessage.IsBodyHtml = true;
            mailMessage.BodyEncoding = UTF8Encoding.UTF8;
            mailMessage.CC.Add(_options.Value.Mailbox);

            // SmtpClient client = new SmtpClient("smtp.office365.com", 587);
            SmtpClient client = new SmtpClient(_options.Value.Server, _options.Value.Port);
            client.EnableSsl = true;

            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            //client.Credentials = new NetworkCredential(senderEmail, senderPassword);
            client.Credentials = new NetworkCredential(_options.Value.Email, _options.Value.Password,

_options.Value.Server);

            //MailMessage mailMessage = new MailMessage(senderEmail, receiverEmail, subject, emailBody);


            await client.SendMailAsync(mailMessage);

            return true;

I need to send emails using an exchange server.

sachind
  • 387
  • 5
  • 8
  • 1
    The question asks how to "create an email service" at one point and how to send an email in another - two completely different things. So either this is out of scope, or asking how to send a plain old email. There are a lot of email-related questions in SO already, have you tried anything? Tried the docs? Googled for `c# send email` ? – Panagiotis Kanavos Oct 05 '21 at 10:20
  • There are SO questions that show how to connect to Office 365 SMTP servers, so this is essentially a duplicate, eg [of this one](https://stackoverflow.com/questions/6244694/send-smtp-email-using-system-net-mail-via-exchange-online-office-365). Have you tried to send an email and got an error? – Panagiotis Kanavos Oct 05 '21 at 10:34
  • Did you get an exception with this code? What is it? Domain credentials aren't emails and servers. Passing the Office 365 server as the local domain name is *definitely* wrong. Try setting the `UseDefaultCredentials` to `true` instead of specifying the credentials explicitly. – Panagiotis Kanavos Oct 05 '21 at 11:11
  • I tried with the solution of your comment, but my error is still same, as below: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. [PN1PR0101CA0007.INDPRD01.PROD.OUTLOOK.COM] Domain credentials are emails and servers. If it is not an email then what to specify for email & server. – sachind Oct 05 '21 at 11:54
  • Solution: Set smtp client default credential to false after setting client. Code:- SmtpClient client = new SmtpClient(MailServer, Port); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(UserEmail, Password); client.Credentials = credentials; client.UseDefaultCredentials = false; – sachind Oct 20 '21 at 07:04

0 Answers0