Question

Je suis nouveau à l'aide de

Était-ce utile?

La solution

Eh bien, j'ai finalement compris cela. Voici une méthode qui permettra de créer un message électronique, le stocker sous forme d'un projet, ajouter la pièce jointe, puis envoyez-le. Espérons que cela aide quelqu'un là-bas qui n'a pas pu trouver un bon exemple comme moi.

Dans mon exemple, je ne transmettrai les fichiers Excel qui est la raison pour laquelle est défini comme le type de contenu est. Cela peut, évidemment, être modifié pour supporter tout type de fichier joint.

Pour votre référence, la variable esb est une variable de niveau de classe de type ExchangeServiceBinding.

Modifier

Il faut aussi noter que, dans cet exemple, je ne suis pas vérifier les types de réponse des actions de change pour le succès ou l'échec. Cela devrait certainement vérifier si vous voulez le savoir si oui ou non vos appels vers SAP effectivement travaillé.

public void SendEmail(string from, string to, string subject, string body, byte[] attachmentAsBytes, string attachmentName)
        {
            //Create an email message and initialize it with the from address, to address, subject and the body of the email.
            MessageType email = new MessageType();

            email.ToRecipients = new EmailAddressType[1];
            email.ToRecipients[0] = new EmailAddressType();
            email.ToRecipients[0].EmailAddress = to;

            email.From = new SingleRecipientType();
            email.From.Item = new EmailAddressType();
            email.From.Item.EmailAddress = from;

            email.Subject = subject;

            email.Body = new BodyType();
            email.Body.BodyType1 = BodyTypeType.Text;
            email.Body.Value = body;

            //Save the created email to the drafts folder so that we can attach a file to it.
            CreateItemType emailToSave = new CreateItemType();
            emailToSave.Items = new NonEmptyArrayOfAllItemsType();
            emailToSave.Items.Items = new ItemType[1];
            emailToSave.Items.Items[0] = email;
            emailToSave.MessageDisposition = MessageDispositionType.SaveOnly;
            emailToSave.MessageDispositionSpecified = true;

            CreateItemResponseType response = esb.CreateItem(emailToSave);
            ResponseMessageType[] rmta = response.ResponseMessages.Items;
            ItemInfoResponseMessageType emailResponseMessage = (ItemInfoResponseMessageType)rmta[0];

            //Create the file attachment.
            FileAttachmentType fileAttachment = new FileAttachmentType();
            fileAttachment.Content = attachmentAsBytes;
            fileAttachment.Name = attachmentName;
            fileAttachment.ContentType = "application/ms-excel";

            CreateAttachmentType attachmentRequest = new CreateAttachmentType();
            attachmentRequest.Attachments = new AttachmentType[1];
            attachmentRequest.Attachments[0] = fileAttachment;
            attachmentRequest.ParentItemId = emailResponseMessage.Items.Items[0].ItemId;

            //Attach the file to the message.
            CreateAttachmentResponseType attachmentResponse = (CreateAttachmentResponseType)esb.CreateAttachment(attachmentRequest);
            AttachmentInfoResponseMessageType attachmentResponseMessage = (AttachmentInfoResponseMessageType)attachmentResponse.ResponseMessages.Items[0];

            //Create a new item id type using the change key and item id of the email message so that we know what email to send.
            ItemIdType attachmentItemId = new ItemIdType();
            attachmentItemId.ChangeKey = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemChangeKey;
            attachmentItemId.Id = attachmentResponseMessage.Attachments[0].AttachmentId.RootItemId;

            //Send the email.
            SendItemType si = new SendItemType();
            si.ItemIds = new BaseItemIdType[1];
            si.SavedItemFolderId = new TargetFolderIdType();
            si.ItemIds[0] = attachmentItemId;
            DistinguishedFolderIdType siSentItemsFolder = new DistinguishedFolderIdType();
            siSentItemsFolder.Id = DistinguishedFolderIdNameType.sentitems;
            si.SavedItemFolderId.Item = siSentItemsFolder;
            si.SaveItemToFolder = true;

            SendItemResponseType siSendItemResponse = esb.SendItem(si);
        }

Autres conseils

Je sais que cette question est très ancienne, mais j'atterri ici après une recherche Google. Voici une réponse mise à jour simplifiée de travail avec l'aide d'instructions.

Vous devez ajouter les Microsoft.Exchange.WebServices de package NuGet à votre projet (la version actuelle est 2.2.0).

using Microsoft.Exchange.WebServices.Data;

namespace Exchange
{
    public static class Emailer
    {
        public static void SendEmail(string from, string to, string subject, string body, byte[] attachmentBytes, string attachmentName)
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            service.AutodiscoverUrl(from);
            var message = new EmailMessage(service)
            {
                Subject = subject,
                Body = body,
            };
            message.ToRecipients.Add(to);
            message.Attachments.AddFileAttachment(attachmentName, attachmentBytes);
            message.SendAndSaveCopy();
        }
    }
}

L'appel à service.AutodiscoverUrl peut prendre plusieurs secondes - si vous connaissez l'URL, vous pouvez éviter d'appeler AutodiscoverUrl et régler directement. (Vous pouvez le récupérer une fois en appelant AutodiscoverUrl puis imprimer service.Url.)

// service.AutodiscoverUrl(from); // This can be slow
service.Url = new System.Uri("https://outlook.domain.com/ews/exchange.asmx");
scroll top