NBug.Helpers.EmailDestinationBuilder.Build C# (CSharp) Method

Build() public method

public Build ( ) : string
return string
        public string Build()
        {
            var sb = new StringBuilder();
              sb.Append("Type=Mail;");
              sb.AppendFormat("From={0};", _fromAddress.Address);

              if (!string.IsNullOrEmpty(_fromName))
              {
            sb.AppendFormat("FromName={0};", _fromName);
              }

              sb.Append("To=");
              foreach (var address in _destinationAddresses)
              {
            sb.AppendFormat("{0},", address.Address);
              }

              sb.Length--;
              sb.Append(";");

              if (_carbonCopyAddresses != null && _carbonCopyAddresses.Length > 0)
              {
            sb.Append("Cc=");
            foreach (var address in _carbonCopyAddresses)
            {
              sb.AppendFormat("{0},", address.Address);
            }

            sb.Length--;
            sb.Append(";");
              }

              if (_blindCarbonCopyAddresses != null && _blindCarbonCopyAddresses.Length > 0)
              {
            sb.Append("Bcc=");
            foreach (var address in _blindCarbonCopyAddresses)
            {
              sb.AppendFormat("{0},", address.Address);
            }

            sb.Length--;
            sb.Append(";");
              }

              if (!string.IsNullOrEmpty(_replyTo))
              {
            sb.AppendFormat("ReplyTo={0};", _replyTo);
              }

              sb.AppendFormat("UseAttachment={0};", _sendAttachments ? "true" : "false");

              if (!string.IsNullOrEmpty(_subject))
              {
            sb.AppendFormat("CustomSubject={0};", _subject);
              }

              if (!string.IsNullOrEmpty(_customBody))
              {
            sb.AppendFormat("CustomBody={0};", _customBody);
              }

              sb.AppendFormat("SmtpServer={0};", _serverName);

              sb.Append("UseSsl=");
              sb.Append(_useSsl ? "true" : "false");
              sb.Append(";");

              sb.AppendFormat("Port={0};", _smtpPort);

              if (_mailPriority.HasValue)
              {
            sb.AppendFormat("Priority={0};", _mailPriority);
              }

              if (string.IsNullOrEmpty(_smtpUserName))
              {
            sb.Append("UseAuthentication=false;");
              }
              else
              {
            sb.Append("UseAuthentication=true;");
            sb.AppendFormat("Username={0};", _smtpUserName);
            sb.AppendFormat("Password={0};", _smtpUserPassword);
              }

              return sb.ToString();
        }

Usage Example

        public void Build_DefaultsToSecuredAnonymousConnectionNoAttachments()
        {
            // Arrange
              var fromAddress = new MailAddress("*****@*****.**");
              var toAddress = new MailAddress("*****@*****.**");
              var anotherAddress = new MailAddress("*****@*****.**");

              // Act
              var builder = new EmailDestinationBuilder(fromAddress, new[] { toAddress, anotherAddress }, ServerName);
              string result = builder.Build();

              // Assert
              var expected =
            string.Format(
              "Type=Mail;From={0};To={1},{2};UseAttachment={3};SmtpServer={4};UseSsl=true;Port=465;UseAuthentication=false;",
              fromAddress.Address,
              toAddress.Address,
              anotherAddress.Address,
              "false",
              ServerName);

              Assert.Equal(expected, result);
        }
All Usage Examples Of NBug.Helpers.EmailDestinationBuilder::Build