Example #1
0
        public void ParseAddresses_WithManyComplexAddresses_ShouldReadCorrectly()
        {
            string addresses = string.Format("{0},{1},{2},{3},{4},{5},{6}",
                                             "\"Dr M\u00FCller\" [email protected]",
                                             "(comment)this.test.this(comment)@(comment)this.test.this(comment)",
                                             "*****@*****.**",
                                             "*****@*****.**",
                                             "(comment)this.test.this(comment)<(comment)this.test.this(comment)@(comment)[  test this ](comment)>",
                                             "\"test\" <*****@*****.**>",
                                             "(comment)\" asciin;,oqu o.tesws \"(comment)<(comment)\" asciin;,oqu o.tesws \"(comment)@(comment)this.test.this(comment)>");

            IList <MailAddress> result = MailAddressParser.ParseMultipleAddresses(addresses);

            Assert.Equal(7, result.Count);

            Assert.Equal("Dr M\u00FCller", result[0].DisplayName);
            Assert.Equal("test", result[0].User);
            Assert.Equal("mail.com", result[0].Host);

            Assert.Equal(string.Empty, result[1].DisplayName);
            Assert.Equal("this.test.this", result[1].User);
            Assert.Equal("this.test.this", result[1].Host);

            Assert.Equal(string.Empty, result[2].DisplayName);
            Assert.Equal("jeff", result[2].User);
            Assert.Equal("example.com", result[2].Host);

            Assert.Equal(string.Empty, result[3].DisplayName);
            Assert.Equal("jeff2", result[3].User);
            Assert.Equal("example.org", result[3].Host);

            Assert.Equal("(comment)this.test.this(comment)", result[4].DisplayName);
            Assert.Equal("this.test.this", result[4].User);
            Assert.Equal("[  test this ]", result[4].Host);

            Assert.Equal("test", result[5].DisplayName);
            Assert.Equal("a..b_b", result[5].User);
            Assert.Equal("example.com", result[5].Host);

            Assert.Equal(" asciin;,oqu o.tesws ", result[6].DisplayName);
            Assert.Equal("\" asciin;,oqu o.tesws \"", result[6].User);
            Assert.Equal("this.test.this", result[6].Host);
        }
Example #2
0
        internal static bool IsValidEmailAddress(string value)
        {
            try
            {
#if uap
                new MailAddress(value);
#else
                MailAddressParser.ParseAddress(value);
#endif
                return(true);
            }
            catch (FormatException e)
            {
                if (NetEventSource.IsEnabled)
                {
                    NetEventSource.Error(null, SR.Format(SR.net_http_log_headers_wrong_email_format, value, e.Message));
                }
            }
            return(false);
        }
Example #3
0
        internal static bool IsValidEmailAddress(string value)
        {
            try
            {
#if NETNative
                new MailAddress(value);
#else
                MailAddressParser.ParseAddress(value);
#endif
                return(true);
            }
            catch (FormatException e)
            {
                if (NetEventSource.Log.IsEnabled())
                {
                    NetEventSource.PrintError(NetEventSource.ComponentType.Http, string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_log_headers_wrong_email_format, value, e.Message));
                }
            }
            return(false);
        }
Example #4
0
        internal static bool IsValidEmailAddress(string value)
        {
            try
            {
#if NET_4
                new MailAddress(value);
#else
                MailAddressParser.ParseAddress(value);
#endif
                return(true);
            }
            catch (FormatException e)
            {
                if (Logging.On)
                {
                    Logging.PrintError(Logging.Http, string.Format(System.Globalization.CultureInfo.InvariantCulture, SR.net_http_log_headers_wrong_email_format, value, e.Message));
                }
            }
            return(false);
        }
Example #5
0
        private async void Send(object sender, EventArgs e)
        {
            Account account = App.AccountManager.GetCurrentAccount();

            if (account == null)
            {
                return;
            }

            // TODO: validate fields

            MailMessage message = new MailMessage();

            message.Date = DateTime.Now;
            message.From = new MailAddress(account.Info.Address, account.Info.DisplayName);
            MailAddressParser.ParseAddressField(ToField.Text.Trim()).ForEach(message.To.Add);
            message.Subject                 = SubjectField.Text.Trim();
            message.ContentType             = "text/plain; charset=utf-8";
            message.ContentTransferEncoding = "quoted-printable";
            message.Body = BodyField.Text;

            foreach (var pair in _additionalHeaders)
            {
                message.Headers.Add(pair.Key, new HeaderValue(pair.Value));
            }

            // TODO: Short term: Progress bar
            ProgressIndicator.IsIndeterminate = true;
            try
            {
                // TODO: Long term: Save to drafts and memory, then send in the background. Retry if no connectivity.
                await account.SendMessageAsync(message);
            }
            finally
            {
                ProgressIndicator.IsIndeterminate = false;
            }

            NavigationService.GoBack();
        }
Example #6
0
        //
        // This constructor validates and stores the components of an e-mail address.
        //
        // Preconditions:
        // - 'address' must not be null or empty.
        //
        // Postconditions:
        // - The e-mail address components from the given 'address' are parsed, which should be formatted as:
        // "EncodedDisplayname" <username@host>
        // - If a 'displayName' is provided separately, it overrides whatever display name is parsed from the 'address'
        // field.  The display name does not need to be pre-encoded if a 'displayNameEncoding' is provided.
        //
        // A FormatException will be thrown if any of the components in 'address' are invalid.
        public MailAddress(string address, string displayName, Encoding displayNameEncoding)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (address == string.Empty)
            {
                throw new ArgumentException(string.Format(Strings.net_emptystringcall, nameof(address)), nameof(address));
            }

            _displayNameEncoding = displayNameEncoding ?? Encoding.GetEncoding(MimeBasePart.DefaultCharSet);
            _displayName         = displayName ?? string.Empty;

            // Check for bounding quotes
            if (!string.IsNullOrEmpty(_displayName))
            {
                _displayName = MailAddressParser.NormalizeOrThrow(_displayName);

                if (_displayName.Length >= 2 && _displayName[0] == '\"' &&
                    _displayName[_displayName.Length - 1] == '\"')
                {
                    // Peal bounding quotes, they'll get re-added later.
                    _displayName = _displayName.Substring(1, _displayName.Length - 2);
                }
            }

            MailAddress result = MailAddressParser.ParseAddress(address);

            _host     = result._host;
            _userName = result._userName;

            // If we were not given a display name, use the one parsed from 'address'.
            if (string.IsNullOrEmpty(_displayName))
            {
                _displayName = result._displayName;
            }
        }
 public void TryParseAddress_WithInvalidLocalPartAtEnd_ShouldThrow()
 {
     Assert.Throws <FormatException>(() => { MailAddressParser.TryParseAddress("[test][email protected]", out ParseAddressInfo _, true); });
 }
 public void TryParseAddress_WithHangingAngleBracket_ShouldThrow()
 {
     Assert.Throws <FormatException>(() => { MailAddressParser.TryParseAddress("<*****@*****.**", out ParseAddressInfo _, true); });
 }
Example #9
0
        private static InboundEmail ParseInboundEmail(IDictionary <Encoding, MultipartFormDataParser> encodedParsers, KeyValuePair <string, Encoding>[] charsets)
        {
            // Get the default UTF8 parser
            var parser = encodedParsers.Single(p => p.Key.Equals(Encoding.UTF8)).Value;

            // Convert the 'headers' from a string into array of KeyValuePair
            var headers = parser
                          .GetParameterValue("headers", string.Empty)
                          .Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries)
                          .Select(header =>
            {
                var splitHeader = header.Split(new[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
                var key         = splitHeader[0];
                var value       = splitHeader.Length >= 2 ? splitHeader[1] : null;
                return(new KeyValuePair <string, string>(key, value));
            }).ToArray();

            // Raw email
            var rawEmail = parser.GetParameterValue("email", string.Empty);

            // Combine the 'attachment-info' and Files into an array of Attachments
            var attachmentInfoAsJObject = JObject.Parse(parser.GetParameterValue("attachment-info", "{}"));
            var attachments             = attachmentInfoAsJObject
                                          .Properties()
                                          .Select(prop =>
            {
                var attachment = prop.Value.ToObject <InboundEmailAttachment>();
                attachment.Id  = prop.Name;

                var file = parser.Files.FirstOrDefault(f => f.Name == prop.Name);
                if (file != null)
                {
                    attachment.Data = file.Data;
                    if (string.IsNullOrEmpty(attachment.ContentType))
                    {
                        attachment.ContentType = file.ContentType;
                    }
                    if (string.IsNullOrEmpty(attachment.FileName))
                    {
                        attachment.FileName = file.FileName;
                    }
                }

                return(attachment);
            }).ToArray();

            // Convert the 'envelope' from a JSON string into a strongly typed object
            var envelope = JsonConvert.DeserializeObject <InboundEmailEnvelope>(parser.GetParameterValue("envelope", "{}"));

            // Convert the 'from' from a string into an email address
            var rawFrom = GetEncodedValue("from", charsets, encodedParsers, string.Empty);
            var from    = MailAddressParser.ParseEmailAddress(rawFrom);

            // Convert the 'to' from a string into an array of email addresses
            var rawTo = GetEncodedValue("to", charsets, encodedParsers, string.Empty);
            var to    = MailAddressParser.ParseEmailAddresses(rawTo);

            // Convert the 'cc' from a string into an array of email addresses
            var rawCc = GetEncodedValue("cc", charsets, encodedParsers, string.Empty);
            var cc    = MailAddressParser.ParseEmailAddresses(rawCc);

            // Arrange the InboundEmail
            var inboundEmail = new InboundEmail
            {
                Attachments = attachments,
                Charsets    = charsets,
                Dkim        = GetEncodedValue("dkim", charsets, encodedParsers, null),
                Envelope    = envelope,
                From        = from,
                Headers     = headers,
                Html        = GetEncodedValue("html", charsets, encodedParsers, null),
                SenderIp    = GetEncodedValue("sender_ip", charsets, encodedParsers, null),
                SpamReport  = GetEncodedValue("spam_report", charsets, encodedParsers, null),
                SpamScore   = GetEncodedValue("spam_score", charsets, encodedParsers, null),
                Spf         = GetEncodedValue("SPF", charsets, encodedParsers, null),
                Subject     = GetEncodedValue("subject", charsets, encodedParsers, null),
                Text        = GetEncodedValue("text", charsets, encodedParsers, null),
                To          = to,
                Cc          = cc,
                RawEmail    = rawEmail
            };

            return(inboundEmail);
        }
Example #10
0
 internal static bool IsValidEmailAddress(string value)
 {
     if (MailAddressParser.TryParseAddress(value, out ParseAddressInfo _, throwExceptionIfFail: false))
     {
         return(true);
     }
Example #11
0
 public void ParseAddress_WithInvalidLocalPartAtEnd_ShouldThrow()
 {
     Assert.Throws <FormatException>(() => { MailAddressParser.ParseAddress("[test][email protected]"); });
 }
Example #12
0
 public void ParseAddress_WithHangingAngleBracket_ShouldThrow()
 {
     Assert.Throws <FormatException>(() => { MailAddressParser.ParseAddress("<*****@*****.**"); });
 }
Example #13
0
 public MailAddress(string address)
 {
     MailAddressParser.ParseAddress(address);
 }