0

I have written below code to send email using Outlook Office365.

ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
myService.Credentials = new WebCredentials(sender_mailId,sender_password);

try
{
  string serviceUrl = <<service url>> // This URL 
  myService.Url = new Uri(serviceUrl);
  EmailMessage emailMessage = new EmailMessage(myservice);
  emailMessage.Subject = "Subject test ";
  emailMessage.Body = new MessageBody("Testing Exchange Web Service API");
  emailMessage.ToRecipients.Add(to_email_id);
  emailMessage.Send();
}
catch (SmtpException exception)
{
  string msg = "Mail cannot be sent (SmtpException):";
  msg += exception.Message;
  throw new Exception(msg);
}

What web service URL should be used?

Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37

1 Answers1

0

The below code is worked to send or save to email draft.

static void CheckEmail()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new WebCredentials(senderEmailId, password);
 service.AutodiscoverUrl(senderEmailId, RedirectionUrlValidationCallback);

 EmailMessage emailMessage = new EmailMessage(service);
 emailMessage.Subject = "Test office 365 project draft ";
 emailMessage.Body = new MessageBody("Testing Exchange Web Service API");

 emailMessage.ToRecipients.Add(emailTo);

//send email
emailMessage.Send();

//save to draft
emailMessage.Save(WellKnownFolderName.Drafts);
}

   private static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }
        return result;
    }