Aller au contenu

C# Connexion Serveur Exchange & envoi de mail

Yooo

Avec l’API Managée EWS , eaaaaaaasy 🙂

A télécharger ici : https://www.microsoft.com/en-us/download/details.aspx?id=42951

Ensuite voici un exemple  en supposant que l’adresse mail sur laquelle je souhaite me connecter est toto@live.fr et le mdp : testtoto

Dans l’exemple ci-dessous vous envoyer un mail de toto@live.fr vers AdresseMailDestinataire@live.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;

namespace ConsoleApplication3
{
 class Program
 {
 static void Main(string[] args)
 {
 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

service.Credentials = new WebCredentials("toto@live.fr", "testtoto");

service.TraceEnabled = true;
 service.TraceFlags = TraceFlags.All;

service.AutodiscoverUrl("toto@live.fr", RedirectionUrlValidationCallback);

EmailMessage email = new EmailMessage(service);

email.ToRecipients.Add("AdresseMailDestinataire@live.com");

email.Subject = "Hello Bibliogeek :-)";
 email.Body = new MessageBody("Test Envoie de mail réussi youhOuuuuuuuuuuuuuuuuuu");

email.Send();
 }

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;
 }
 }
 }

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *