Exemplo n.º 1
0
        /// <summary>
        /// Saves the Message received by the SMTP
        /// </summary>
        /// <param name="context">The context information</param>
        /// <param name="transaction">The transaction</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns></returns>
        public override Task <SmtpServer.Protocol.SmtpResponse> SaveAsync(ISessionContext context, IMessageTransaction transaction, CancellationToken cancellationToken)
        {
            // The Routable Message
            RoutableMessage routableMessage = new RoutableMessage();

            routableMessage.CreationDateTime = DateTime.Now;

            try
            {
                // Retrieve the Mail From
                routableMessage.MailFrom = new MailboxAddress(string.Format("{0}@{1}",
                                                                            transaction.From.User,
                                                                            transaction.From.Host));

                // Retrieve the Mail To
                foreach (var mailTo in transaction.To)
                {
                    routableMessage.Recipients.Add(new MailboxAddress($"{mailTo.User}@{mailTo.Host}"));
                }

                // Gets the Message Contents and parse it to a MimeMessage
                var textMessage = (ITextMessage)transaction.Message;
                routableMessage.Message = MimeKit.MimeMessage.Load(textMessage.Content);

                // Retrieve information for the ReceivedBy header
                var sourceIpAddress  = ((IPEndPoint)context.RemoteEndPoint).Address.ToString();
                var machineIpAddress = GetLocalIP();
                var machineHostName  = System.Net.Dns.GetHostName();

                var receiveByString = string.Format("from [{0}] ({0}) by {1} ({2}) with Smtp Router Service; {3}",
                                                    sourceIpAddress,
                                                    machineHostName,
                                                    machineIpAddress,
                                                    DateTime.Now.ToString("ddd, dd MMM yyy HH:mm:ss %K"));

                // Append Received-By to the MimeMessage Header
                routableMessage.Message.Headers.Add("X-SM-Received", receiveByString);

                // Set the IP Address
                routableMessage.IPAddress = sourceIpAddress;

                // Trigger Event to inform a message was received
                MessageReceived?.Invoke(this, new MessageEventArgs(routableMessage));
            }
            catch (Exception e)
            {
                // Notify listener
                MessageReceivedWithErrors?.Invoke(this, new MessageErrorEventArgs(routableMessage, e));

                // Something failed
                return(Task.FromResult(SmtpServer.Protocol.SmtpResponse.TransactionFailed));
            }

            // Transaction was processed successfully
            return(Task.FromResult(SmtpServer.Protocol.SmtpResponse.Ok));
        }
Exemplo n.º 2
0
 private void SmtpMessageStore_MessageReceivedWithErrors(object sender, MessageErrorEventArgs e)
 {
     // Trigger the MessageReceivedWithErrors for the Listener
     MessageReceivedWithErrors?.Invoke(sender, e);
 }