[E2010][C#][TransportAgent] - RoutingAgent - Reject - Change entire message? RRS feed

  • Question

  • What I am trying to do is reject specific recipients with customized messages.
    Using the "OnSubmittedMessage" example froM MSFT: is not giving me the desired output.
    When I Reject a message (SampleRoutingAgent.rejectResponse) it sends not only my custom response, but about a page of the standard NDR type information.

    What I would like to do is have a very simple response that states "Employee <their name> no longer works for our firm.  Please call <number> to reach our front desk or <secondary number> to reach their manager."

    Can this be done using a DSNType.Failure?  Or will that always send the extra stuff (including the original e-mail as an attachment, etc).
    If not, is it possible to initiate my own *reply* e-mail and send back?

    I have yet to see any examples of anyone trying this.

    Saturday, March 12, 2011 12:08 AM

All replies

  • DSN are generated in nearly all cases by the sending mail daemon so the the actually DSN that a user would receive would depend on the senders mail system all your going to control using that method is the Status code and DNS Message sent back to the sending server. If you want that level of control you will need to generate a new normal message and send that back to sender using information within the message you received being careful not to generate a mail loop in the process.


    Monday, March 14, 2011 2:25 AM
  • Thanks.

    I took that and tried to work with it; however, the way that I'm trying to do it doesn't allow me to modify the body of the e-mail.


    EmailMessage origMsg = e.MailItem.Message;
                EmailMessage newMsg = EmailMessage.Create();
                newMsg.To.Add(new EmailRecipient(
                newMsg.From = new EmailRecipient("Rejected Email",
                newMsg.Subject = "Rejected";
                newMsg.Body = "Please call .....";
                SaveMessage(newMsg, String.Concat(PickupPath, "newMsg.eml"));
    public static void SaveMessage(EmailMessage msg, string filePath)
            FileStream file = new FileStream(filePath, System.IO.FileMode.Create);

    The thought is that I create a new e-mail message with the TO: being the FROM: from the original, I then would save it as a .eml file in the pickup directory.

    The logic works - a response goes out - but when I try to modify the .body field it errors out because it is read-only. 


    Monday, March 14, 2011 3:01 PM
  • If you want to write the body with that particular class you would need to use MIMEWritter class which is a fairly hard way to go about it i would just use the System.Net.Mail class instead


    Tuesday, March 15, 2011 2:21 AM
  • Thank you for all of the great responses.
    Would using the System.Net.Mail class be a more efficient way of handling this, generally speaking?

    What I ended up doing before I noticed this, was using a simple Stream Writer and just appending the body into the .eml message that I created.

     EmailMessage origMsg = e.MailItem.Message;
              EmailMessage newMsg = EmailMessage.Create();
              newMsg.To.Add(new EmailRecipient(origMsg.Sender.DisplayName, origMsg.Sender.SmtpAddress));
              newMsg.From = new EmailRecipient("Do Not Reply", "");
              newMsg.Subject = "RE: " + origMsg.Subject;
                FileStream file = new FileStream(String.Concat(PickupPath, recipient.Address.LocalPart + "newMsg.eml"), System.IO.FileMode.Create);
                byte[] msgBody = System.Text.Encoding.ASCII.GetBytes(this.ShouldDropRecipient(e.MailItem, recipient));
                file.Write(msgBody, 0, msgBody.Length);
    Tuesday, March 15, 2011 3:19 PM
  • >> Would using the System.Net.Mail class be a more efficient way of handling this, generally speaking?

    Its splitting hairs really it certainly a lot easier the one thing to be careful of in a transport agent if your going to be doing any Asynchronous I/O that you take note of issues raised in that should help avoid performance problems.

    I don't see a problem with the method your using depending on mail volume and the number of messages your expect to be sent this should be okay. One area of concern maybe if there a sudden spike in the volume of messages could this cause your agent to DOS your edge or hub server because of the File I/O probably not but it something to consider.



    Wednesday, March 16, 2011 2:22 AM
  • Hi,

    DSN details are completed by the sender's server on receipt of the DSN so don't bother.

    As for using System.Net.Mail ?? That's crazy talk.

    It's quite simple:

    • Note details from the received email that you need to compose the response message... sender... recipient/s... subject... whatever.
    • Delete the email using the SubmittedMessageEventSource object's "Delete" function (passed to you as the 1st parameter in OnSubmittedMessage). Note this will delete w/o an DSN (which is what you want).
    • Create a new email using EmaiMessage... populate it with the details you saved (above) and post it back to the originator using the SmtpServer object's SubmitMessage function (this object was passed to you when the agent factory called "CreateAgent" - you'll need to save a copy to use it in OnSubmittedMessage).
    • As an alternative to SmtpServe's SubmitMessage function you could compose a fresh message as a text file and drop it into the Pickup or Replay directory on the Exchange. For details on this google (or Bing) "Using the pickup directory" or "Using the replay directory".


    Scott Quinn

    Monday, March 21, 2011 6:39 AM
  • One more question :).  Thank you all for your help with this.

    My system administrator is concerned about the impact of creating a new e-mail message and sending it off, vs rejecting a recipient.
    The concern revolves around our possibility of getting black listed due to "spam."  If, perhaps, the person sending to us was a spoofed address, a rejection would notify the true sender; however, a new e-mail would go to the spoofed "fake" sender.

    Any thoughts on this?  Real world concern?  Best way to do this?

    Tuesday, March 29, 2011 9:53 PM
  • Its depends on your inbound mail configuration and how you filter spam at the moment eg do you use a external service like postini or forefront. If you consider all the email that a mail server could potential receive as a general rule 90% will be spam and around 10 % will be valid and even within that you have bulk mailers for valid mailing etc it could be a problem if you respond to every message or you start sending email back to bulk senders because a normal mail server wouldn't do that outside of auto-responder. Really you only want to respond to those people that where contacts of the mailbox of the person who no longer works there and you only want to respond once.  It comes down to how well you design the process eg if what you come up with is the same as leaving the mailbox there with a automatic response then you shouldn't really have too many problem (as this would be in a sense a supported scenario), but if its not designed well and say for instance you change the operation of your server so its no longer RFC compliant in the way DSN are designed and tested to work then you could potentially be creating a lot problems. 

    What gets a server blacklisted is more in relation to the content of Messages (mass marketing) or the configuration of the server (open relay) etc.


    Wednesday, March 30, 2011 1:56 AM