Custom Transport Agent - holding email in mailbox

171 views Asked by At

I am developing a custom transport agent in C#.I am using microsoft exhange server 2010 for this. Whenever an emails comes i need to decide if it should allow to go or not on the basis of some condition. If it should not be allowed to go i store whole email(.eml) as blob in database.Now i want not to use database for holding rejected emails. i want to hold those emails in some mailbox. I want to know if it is possible to redirect an email to mailbox?

1

There are 1 answers

0
lantsev1981 On
public class FIMFactory : RoutingAgentFactory

{

public override RoutingAgent CreateAgent(SmtpServer server)
{
    return new FIMAgent(server);
}

}

public class FIMAgent : RoutingAgent { private readonly SmtpServer _server;

public FIMAgent(SmtpServer server)
{
    _server = server ?? throw new ArgumentNullException(nameof(server));
    OnResolvedMessage += FIMAgent_OnResolvedMessage;
}

private void FIMAgent_OnResolvedMessage(ResolvedMessageEventSource source, QueuedMessageEventArgs e)
{ 
    var mi = e.MailItem;
    var emailMessage = EmailMessage.Create(BodyFormat.Text, false, "utf-8");
    emailMessage.Sender = new EmailRecipient(mi.Message.Sender.DisplayName, mi.Message.Sender.SmtpAddress);
    emailMessage.To.Add(new EmailRecipient(mi.Message.Sender.DisplayName, mi.Message.Sender.SmtpAddress));
    emailMessage.Subject = "...";
    
    using (var stream1 = emailMessage.Body.GetContentWriteStream())
    using (var sw = new StreamWriter(stream1, new UTF8Encoding(false)))
    {
        sw.WriteLine("...");
    }
    
    _server.SubmitMessage(emailMessage);
}

}