Ejemplo n.º 1
0
		public void SendMessage(MailMessage message, String origin, String[] destination)
		{
			if (origin.Length == 0) throw
				new ArgumentException("No origin provided", "origin");

			if (destination.Length == 0)
				throw new ArgumentException("No destination provided", "destination");

			if (!this.SendAndWaitForResponse(String.Format("MAIL FROM: <{0}>", origin), 250))
				throw new Exception(String.Format("Invalid mail from reply: {0} {1}", this.LastResponseNo, this.LastResponseText));

			for (Int32 i = 0; i < destination.Length; i++)
				if (!this.SendAndWaitForResponse(String.Format("RCPT TO: <{0}>", destination[i]), 250))
					throw new Exception(String.Format("Invalid rcpt to reply: {0} {1}", this.LastResponseNo, this.LastResponseText));

			if (!this.SendAndWaitForResponse("DATA", 354))
				throw new Exception(String.Format("Invalid data reply: {0} {1}", this.LastResponseNo, this.LastResponseText));

			MemoryStream lStr = new MemoryStream();

			message.EncodeMessage(lStr);
			lStr.Position = 0;
			lStr.WriteTo(this.CurrentConnection);
			if (!this.SendAndWaitForResponse(".", 250))
				throw new Exception(String.Format("Invalid data reply: {0} {1}", this.LastResponseNo, this.LastResponseText));
		}
Ejemplo n.º 2
0
        public void SendMessage(MailMessage message)
        {
            List<String> lAddresses = new List<String>();

            SmtpClient.EnlistMailAddresses(lAddresses, message.To);
            SmtpClient.EnlistMailAddresses(lAddresses, message.Cc);
            SmtpClient.EnlistMailAddresses(lAddresses, message.Bcc);

            this.SendMessage(message, message.From.Address, lAddresses.ToArray());
        }
Ejemplo n.º 3
0
		public static Boolean NeedEncoding(MailMessage message)
		{
			if (null == message)
				throw new ArgumentNullException();

			Boolean lIsUnicode = (-1 == ContainsUnicodeSymbols(message.Subject));
			if (!lIsUnicode)
				return true;

			lIsUnicode = (-1 == ContainsUnicodeSymbols(message.Contents));
			if (!lIsUnicode)
				return true;

			return true;
		}
Ejemplo n.º 4
0
		public void DecodeMessage(MailMessage destination, Stream source)
		{
			TextReader lReader = new StreamReader(source);
			String lMessage = lReader.ReadToEnd();
			String lBody;

			//Debug.Write(lMessage);
			if (lMessage.StartsWith(CRLF))
			{
				lBody = lMessage.Substring(2);
			}
			else
			{
				Int32 lHeaderEnd = lMessage.IndexOf(CRLF + CRLF, StringComparison.Ordinal);
				if (lHeaderEnd < 0)
				{
					throw (new Exception("Invalid email message"));
				}

				ParseHeader(destination, lMessage.Substring(0, lHeaderEnd));

				lBody = lMessage.Substring(lHeaderEnd + 4);
			}

			destination.Contents = lBody;
		}
Ejemplo n.º 5
0
		private void ParseHeader(MailMessage message, String header)
		{
			StringReader lStringReader = new StringReader(header);
			HeaderField lField = null;

			String lLine;
			while (!String.IsNullOrEmpty((lLine = lStringReader.ReadLine())))
			{
				if ((lLine[0] == ' ' || lLine[0] == '\t') && (lField != null))
				{ // header continuation or property
					lField.Value += lLine;
				}
				else
				{ // header
					Int32 lPosition = lLine.IndexOf(':');
					String lName = lLine.Substring(0, lPosition);
					String lValue;

					if ((lPosition + 2) < lLine.Length)
						lValue = lLine.Substring(lPosition + 2);
					else
						lValue = String.Empty;

					lField = new HeaderField(RemoveQuotes(lValue));
					message.Fields.Add(lName, lField);
				}
			}

			lField = message.Fields["To"];
			if (lField != null)
			{
				message.Fields.Remove("To");
				SetAddresses(message.To, lField.Value);
			}

			lField = message.Fields["From"];
			if (lField != null)
			{
				message.Fields.Remove("From");
				message.From.FromString(lField.Value);
			}

			lField = message.Fields["Sender"];
			if (lField != null)
			{
				message.Fields.Remove("Sender");
				message.Sender.FromString(lField.Value);
			}

			lField = message.Fields["CC"];
			if (lField != null)
			{
				message.Fields.Remove("CC");
				SetAddresses(message.Cc, lField.Value);
			}

			lField = message.Fields["bcc"];
			if (lField != null)
			{
				message.Fields.Remove("bcc");
				SetAddresses(message.Bcc, lField.Value);
			}
		}
Ejemplo n.º 6
0
		public void EncodeMessage(MailMessage source, Stream destination)
		{
			StreamWriter lWrter = new StreamWriter(destination);

			if (DefaultEncoder.NeedEncoding(source))
				lWrter.WriteLine("MIME-Version: 1.0");

			for (Int32 i = 0; i < source.Fields.Count; i++)
			{
				String lKey = source.Fields.Keys[i];
				HeaderField lValue = source.Fields[lKey];

				if (lKey.Equals("Subject"))
				{
					lValue.Value = DefaultEncoder.EncodeUtf8Base64(lValue.Value);
					lWrter.WriteLine(lKey + ": " + lValue);
					continue;
				}

				lWrter.WriteLine(SplitMultiLine(lKey + ": " + lValue, 80));
			}

			String lFromEncoded = DefaultEncoder.EncodeUtf8Base64(source.From.Name);
			String lFrom = String.Format("From: {0} <{1}>", lFromEncoded, source.From.Address);

			lWrter.WriteLine(lFrom);

			if (source.Sender.IsSet())
				lWrter.WriteLine(SplitMultiLine("Sender: " + source.Sender, 80));

			if (source.To.Count > 0)
				lWrter.WriteLine(SplitMultiLine("To: " + source.To, 80));

			if (source.Cc.Count > 0)
				lWrter.WriteLine(SplitMultiLine("Cc: " + source.Cc, 80));

			if (-1 != DefaultEncoder.ContainsUnicodeSymbols(source.Contents))
			{
				lWrter.WriteLine("Content-Type: text/plain; charset=UTF-8; format=flowed");
				lWrter.WriteLine("Content-Transfer-Encoding: 8bit");
			}

			lWrter.WriteLine();
			lWrter.Write(source.Contents);

			if (!source.Contents.EndsWith("\r\n")) // always add an enter
				lWrter.WriteLine();

			lWrter.Flush();
		}
Ejemplo n.º 7
0
 public abstract void DecodeMessage(MailMessage destination, Stream source);
Ejemplo n.º 8
0
 public abstract void EncodeMessage(MailMessage source, Stream destination);
Ejemplo n.º 9
0
		public MailMessage GetHeaders(Int32 messageIndex, Int32 messageNumber)
		{
			String lCommand = "TOP ";

			if (messageNumber == -1)
				lCommand += messageIndex.ToString();
			else
				lCommand += messageNumber.ToString();

			lCommand += " 1";

			String lResponse = SendAndReceive(lCommand);

			if (lResponse.StartsWith("-ERR"))
				throw new Exception(String.Format("Could not retrieve message at index {0}: {1}", messageIndex, lResponse));

			MailMessage lMessage = new MailMessage();
			MemoryStream lStream = new MemoryStream();

			using (StreamWriter writer = new StreamWriter(lStream))
			{
				while (lResponse != ".")
				{
					lResponse = this.CurrentConnection.ReadLine();
					writer.WriteLine(lResponse);
				}

				writer.Flush();

				lStream.Position = 0;
				lMessage.DecodeMessage(lStream);
			}

			return lMessage;
		}