+2 votes

Purchased the other day. Having great success.
Writing a server relay app.

Is there a way to override RCPT TO?

by

1 Answer

0 votes
 
Best answer

You can use SmtpMail class for that:

This code creates an email:

MailBuilder builder = new MailBuilder();
builder.Subject = "Hello";
builder.From.Add(new MailBox("alice@example.com"));
builder.To.Add(new MailBox("bob@example.org"));
IMail email = builder.Create();

This is what normally Smtp class does, when SendMessage method is invoked with IMail parameter:

SmtpMail smtpMail = SmtpMail.CreateFrom(email);
Assert.AreEqual("alice@example.com", smtpMail.From);

You can then change From to anything you'd like:

smtpMail.From = "anyhting@example.com";
Assert.AreEqual("anyhting@example.com", smtpMail.From);

You can the send this email:

using(Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");  // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");

    smtp.SendMessage(smtpMail); // Note SmtpMail usage here

    smtp.Close();    
}

When sending MDNs (from must be null), you must set IsMDN to true, so
validation doesn't kick in:

SmtpMail smtpMail = SmtpMail.CreateFrom(email);
smtpMail.IsMDN = true;
smtpMail.From = null;
Assert.AreEqual(null, smtpMail.From);

If you plan to use VERP:

SmtpMail smtpMail = SmtpMail.CreateUsingVERP(email);
Assert.AreEqual("alice+bob=example.org@example.com", smtpMail.From);
by (301k points)
...