locked
Trying to get EWS Code to Compile Error GetAttachments.exe does not contain a static 'Main' method suitable for an entry point_ RRS feed

  • Question

  • this is the only error I get when i go to try to compile:

    GetAttachments.exe does not contain a static 'Main' method suitable for an entry point   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Exchange.WebServices.Data;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.IO;

    namespace GetAttachments

    {
           class Program

           {


               public void Main(string[] args, ExchangeService service, ItemId itemId)
                {


                    service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                    service.EnableScpLookup = false;
                    service.Credentials = new WebCredentials("USN", "Password", "domain");

                    service.TraceEnabled = true;
                    service.TraceFlags = TraceFlags.All;
                    service.Url = new Uri("https://<IP address of Exchange Server>/ews/exchange.asmx");
                    ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
                    EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(ItemSchema.Attachments));
                    foreach (Attachment attachment in message.Attachments)
                    {
                        if (attachment is FileAttachment)
                        {
                            FileAttachment fileAttachment = attachment as FileAttachment;

                            // Load the attachment into a file.
                            // This call results in a GetAttachment call to EWS.
                            fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);

                            Console.WriteLine("File attachment name: " + fileAttachment.Name);
                        }
                        else // Attachment is an item attachment.
                        {
                            ItemAttachment itemAttachment = attachment as ItemAttachment;

                            // Load attachment into memory and write out the subject.
                            // This does not save the file like it does with a file attachment.
                            // This call results in a GetAttachment call to EWS.
                            itemAttachment.Load();

                            Console.WriteLine("Item attachment name: " + itemAttachment.Name);
                        }
                    }
                }

                private static bool CertificateValidationCallBack(
                object sender,
                System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                System.Security.Cryptography.X509Certificates.X509Chain chain,
                System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    // If the certificate is a valid, signed certificate, return true.
                    if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
                    {
                        return true;
                    }

                    // If there are errors in the certificate chain, look at each error to determine the cause.
                    if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
                    {
                        if (chain != null && chain.ChainStatus != null)
                        {
                            foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                            {
                                if ((certificate.Subject == certificate.Issuer) &&
                                   (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                                {
                                    // Self-signed certificates with an untrusted root are valid.
                                    continue;
                                }
                                else
                                {
                                    if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                                    {
                                        // If there are any other errors in the certificate chain, the certificate is invalid,
                                        // so the method returns false.
                                        return false;
                                    }
                                }
                            }
                        }

                        // When processing reaches this line, the only errors in the certificate chain are
                        // untrusted root errors for self-signed certificates. These certificates are valid
                        // for default Exchange server installations, so return true.
                        return true;
                    }
                    else
                    {
                        // In all other cases, return false.
                        return true;
                    }
                }
            }
        }

                                    
    Monday, July 6, 2015 3:04 PM