Exemple #1
0
 public static void Submit(SendAction sendAction, string host, int port, NetworkCredential credentials)
 {
     if (string.IsNullOrEmpty(host) || port == 0)
     {
         throw new SmtpServerInfoMissingException();
     }
     using (SmtpTalk smtpTalk = new SmtpTalk(SmtpClientHelper.DebugOutput))
     {
         smtpTalk.Connect(host, port);
         smtpTalk.Ehlo();
         smtpTalk.StartTls(false);
         smtpTalk.Ehlo();
         smtpTalk.Authenticate(credentials, SmtpSspiMechanism.Login);
         smtpTalk.MailFrom("<" + credentials.UserName + ">", null);
         foreach (string str in sendAction.Recipients)
         {
             smtpTalk.RcptTo("<" + str + ">", null);
         }
         byte[] data = sendAction.Data;
         using (MemoryStream memoryStream = new MemoryStream(data.Length))
         {
             memoryStream.Write(data, 0, data.Length);
             memoryStream.Position = 0L;
             smtpTalk.Chunking(memoryStream);
         }
         smtpTalk.Quit();
     }
 }
        protected override void InternalProcessRecord()
        {
            if (this.probeGuid == Guid.Empty)
            {
                this.probeGuid = Guid.NewGuid();
            }
            SystemProbeMailProperties systemProbeMailProperties = new SystemProbeMailProperties();

            systemProbeMailProperties.Guid = this.ProbeGuid;
            try
            {
                SmtpTalk smtpTalk = new SmtpTalk(new SendSystemProbeEmail.SmtpClientDebugOutput(this));
                smtpTalk.Connect(this.smtpServer, this.port ?? 25);
                smtpTalk.Ehlo();
                SmtpResponse ehloResponse;
                if (SmtpResponse.TryParse(smtpTalk.EhloResponseText, out ehloResponse))
                {
                    this.ehloOptions.ParseResponse(ehloResponse, new IPAddress(0L));
                }
                if (this.UseSsl)
                {
                    smtpTalk.StartTls(true);
                    smtpTalk.Ehlo();
                }
                if (!string.IsNullOrEmpty(this.smtpUser))
                {
                    smtpTalk.Authenticate(new NetworkCredential(this.smtpUser, this.smtpPassword), SmtpSspiMechanism.Kerberos);
                }
                else if (this.UseSsl)
                {
                    smtpTalk.Authenticate(CredentialCache.DefaultNetworkCredentials, SmtpSspiMechanism.Kerberos);
                }
                if (this.ehloOptions.XSysProbe)
                {
                    base.WriteVerbose(Strings.SendingGuidInMailFrom);
                    smtpTalk.MailFrom(string.Format(CultureInfo.InvariantCulture, "{0} xsysprobeid={1}", new object[]
                    {
                        this.from,
                        this.probeGuid.ToString()
                    }), null);
                }
                else
                {
                    smtpTalk.MailFrom(this.from, null);
                }
                this.to.ForEach(delegate(string to)
                {
                    smtpTalk.RcptTo(to, new bool?(false));
                });
                string text = Path.Combine(Path.GetTempPath(), this.probeGuid.ToString());
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
                List <string> list = new List <string>(Directory.GetFiles(text, "*.eml"));
                list.ForEach(delegate(string f)
                {
                    File.Delete(f);
                });
                MailMessage message = this.CreateMailMessage();
                using (SmtpClient smtpClient = new SmtpClient(this.smtpServer))
                {
                    smtpClient.DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = text;
                    smtpClient.Send(message);
                }
                list = new List <string>(Directory.GetFiles(text, "*.eml"));
                if (list.Count != 1)
                {
                    throw new Exception("Unexpected number of files in private pickup folder");
                }
                using (FileStream fileStream = File.OpenRead(list[0]))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        fileStream.CopyTo(memoryStream);
                        memoryStream.Position = 0L;
                        smtpTalk.Chunking(memoryStream);
                    }
                }
                if (this.testContext)
                {
                    systemProbeMailProperties.MailMessage = list[0];
                }
                else
                {
                    try
                    {
                        Directory.Delete(text, true);
                    }
                    catch (IOException ex)
                    {
                        base.WriteWarning(ex.Message);
                    }
                }
                try
                {
                    smtpTalk.Quit();
                }
                catch (SocketException)
                {
                }
                catch (IOException)
                {
                }
            }
            catch (MustBeTlsForAuthException ex2)
            {
                base.WriteWarning(ex2.Message);
            }
            catch (UnexpectedSmtpServerResponseException ex3)
            {
                base.WriteWarning(ex3.Message);
            }
            base.WriteObject(systemProbeMailProperties);
        }