Exemplo n.º 1
0
		public bool Send() {
			IPAddress dnsServerAddress = GetDnsAddress();
			if (dnsServerAddress == null) {
				logger.Error("cannot find DNS-Server address!");
				return false;
			}

			string mailDomain = String.Empty;
			Match mailDomainMatch = Regex.Match(this.RecipientTo, "@(.+)", RegexOptions.Compiled);
			if (mailDomainMatch.Success) {
				mailDomain = mailDomainMatch.Groups[1].Value;
			} else {
				return false;
			}

			MXRecord[] records = Resolver.MXLookup(mailDomain, dnsServerAddress);
			if (records.Length == 0) {
				return false;
			}

			bool result = false;
			foreach(MXRecord record in records) {
				TcpClient client = new TcpClient(record.DomainName, eMailServer.Options.SmtpPort);
				if (!client.Connected) {
					continue;
				}
				SmtpRequestHandler requestHandler = new SmtpRequestHandler(client, eMailServer.Options.SecureSmtpPort, false);
				requestHandler.Connected += (object sender, TcpRequestHandler.TcpRequestEventArgs e) => {
					if (eMailServer.Options.Verbose) {
						logger.Info("Connected to " + e.RemoteEndPoint.Address.ToString() + ":" + e.RemoteEndPoint.Port);
					}
				};
				requestHandler.Disconnected += (object sender, TcpRequestHandler.TcpRequestEventArgs e) => {
					if (eMailServer.Options.Verbose) {
						logger.Info("Disconnected from " + e.RemoteEndPoint.Address.ToString() + ":" + e.RemoteEndPoint.Port);
					}
				};
				requestHandler.CommandReceived += (object sender, SmtpCommandReceivedEventArgs e) => {
					Console.WriteLine("command received => status: " + e.Status + "; message: " + e.Message);
					switch(e.Status) {
						case 220:
							requestHandler.SendMessage(this.MailFromDomain, "EHLO");
							break;
						
						case 250:
							switch(this._state) {
								case TcpRequestHandler.State.MailFromSent:
									requestHandler.SendMessage("RCPT TO:<" + this.RecipientTo + ">");
									this._state = TcpRequestHandler.State.RecipientToSent;
									break;
								
								case TcpRequestHandler.State.RecipientToSent:
									requestHandler.SendMessage("DATA");
									this._state = TcpRequestHandler.State.Default;
									break;
								
								case TcpRequestHandler.State.MessageSent:
									requestHandler.SendMessage("QUIT");
									requestHandler.Close();
									this._state = TcpRequestHandler.State.Default;
									break;
								
								case TcpRequestHandler.State.Default:
									if (e.Message == "STARTTLS") {
										requestHandler.StartTls();
										//if (requestHandler.StartTls()) {
										//Console.WriteLine("send message Go ahead");
										//requestHandler.SendMessage("Go ahead", "220");
										//Console.WriteLine("message Go ahead was sent");
										//}
									}
									requestHandler.SendMessage("MAIL FROM:<" + this.MailFrom + ">");
									this._state = TcpRequestHandler.State.MailFromSent;
									break;
							}
							break;
							
						case 354: // Start Mail Input
							requestHandler.SendMessage("From: <" + this.MailFrom + ">");
							requestHandler.SendMessage("To: <" + this.RecipientTo + ">");
							requestHandler.SendMessage("Subject: <" + this.Subject + ">");
							requestHandler.SendMessage("");
							requestHandler.SendMessage(this.Message);
							requestHandler.SendMessage(".");
							this._state = TcpRequestHandler.State.MessageSent;
							break;
							
						case 530: // Authentication required
							break;
						
						case 550:
							requestHandler.SendMessage("QUIT");
							requestHandler.Close();
							break;
							
						case 554:
							requestHandler.SendMessage("QUIT");
							requestHandler.Close();
							break;
					}
				};
				requestHandler.Start();
				requestHandler.WaitForClosing();
				result = true;
				break;
			}

			return result;
		}
Exemplo n.º 2
0
        public bool Send()
        {
            IPAddress dnsServerAddress = GetDnsAddress();

            if (dnsServerAddress == null)
            {
                logger.Error("cannot find DNS-Server address!");
                return(false);
            }

            string mailDomain      = String.Empty;
            Match  mailDomainMatch = Regex.Match(this.RecipientTo, "@(.+)", RegexOptions.Compiled);

            if (mailDomainMatch.Success)
            {
                mailDomain = mailDomainMatch.Groups[1].Value;
            }
            else
            {
                return(false);
            }

            MXRecord[] records = Resolver.MXLookup(mailDomain, dnsServerAddress);
            if (records.Length == 0)
            {
                return(false);
            }

            bool result = false;

            foreach (MXRecord record in records)
            {
                TcpClient client = new TcpClient(record.DomainName, eMailServer.Options.SmtpPort);
                if (!client.Connected)
                {
                    continue;
                }
                SmtpRequestHandler requestHandler = new SmtpRequestHandler(client, eMailServer.Options.SecureSmtpPort, false);
                requestHandler.Connected += (object sender, TcpRequestHandler.TcpRequestEventArgs e) => {
                    if (eMailServer.Options.Verbose)
                    {
                        logger.Info("Connected to " + e.RemoteEndPoint.Address.ToString() + ":" + e.RemoteEndPoint.Port);
                    }
                };
                requestHandler.Disconnected += (object sender, TcpRequestHandler.TcpRequestEventArgs e) => {
                    if (eMailServer.Options.Verbose)
                    {
                        logger.Info("Disconnected from " + e.RemoteEndPoint.Address.ToString() + ":" + e.RemoteEndPoint.Port);
                    }
                };
                requestHandler.CommandReceived += (object sender, SmtpCommandReceivedEventArgs e) => {
                    Console.WriteLine("command received => status: " + e.Status + "; message: " + e.Message);
                    switch (e.Status)
                    {
                    case 220:
                        requestHandler.SendMessage(this.MailFromDomain, "EHLO");
                        break;

                    case 250:
                        switch (this._state)
                        {
                        case TcpRequestHandler.State.MailFromSent:
                            requestHandler.SendMessage("RCPT TO:<" + this.RecipientTo + ">");
                            this._state = TcpRequestHandler.State.RecipientToSent;
                            break;

                        case TcpRequestHandler.State.RecipientToSent:
                            requestHandler.SendMessage("DATA");
                            this._state = TcpRequestHandler.State.Default;
                            break;

                        case TcpRequestHandler.State.MessageSent:
                            requestHandler.SendMessage("QUIT");
                            requestHandler.Close();
                            this._state = TcpRequestHandler.State.Default;
                            break;

                        case TcpRequestHandler.State.Default:
                            if (e.Message == "STARTTLS")
                            {
                                requestHandler.StartTls();
                                //if (requestHandler.StartTls()) {
                                //Console.WriteLine("send message Go ahead");
                                //requestHandler.SendMessage("Go ahead", "220");
                                //Console.WriteLine("message Go ahead was sent");
                                //}
                            }
                            requestHandler.SendMessage("MAIL FROM:<" + this.MailFrom + ">");
                            this._state = TcpRequestHandler.State.MailFromSent;
                            break;
                        }
                        break;

                    case 354:                             // Start Mail Input
                        requestHandler.SendMessage("From: <" + this.MailFrom + ">");
                        requestHandler.SendMessage("To: <" + this.RecipientTo + ">");
                        requestHandler.SendMessage("Subject: <" + this.Subject + ">");
                        requestHandler.SendMessage("");
                        requestHandler.SendMessage(this.Message);
                        requestHandler.SendMessage(".");
                        this._state = TcpRequestHandler.State.MessageSent;
                        break;

                    case 530:                             // Authentication required
                        break;

                    case 550:
                        requestHandler.SendMessage("QUIT");
                        requestHandler.Close();
                        break;

                    case 554:
                        requestHandler.SendMessage("QUIT");
                        requestHandler.Close();
                        break;
                    }
                };
                requestHandler.Start();
                requestHandler.WaitForClosing();
                result = true;
                break;
            }

            return(result);
        }