+1 vote

I am trying to implement the Smtp.SendMessage method myself using the Smtp class. I cannot seem to figure out how to correctly send the data command.

The only reason I can't use the original SendMessage is that I want to change how the RCPTO command is send.

If there is more than a single "To", I still want only a single RCPTO command sent to the server with a separate email. Is there
any way to do that?

by

1 Answer

0 votes
 
Best answer

You should rather use Smtp.SendMessage method overload, that takes SmtpMail instance as parameter:

public SendMessageResult SendMessage(SmtpMail smtpMail)

You can modify SmtpMail instance according to your needs before each send. You can add/remove items from SmtpMail.To collection and use Smtp for actual sending.

IMail email = ...
SmtpMail smtpMail = SmtpMail.CreateFrom(email);

string first = smtpMail.To[0];
smtpMail.To.Clear();
smtpMail.To.Add(first);

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

    smtp.SendMessage(smtpMail);                     

    smtp.Close();   
}        
by (301k points)
...