by Dave
27. September 2011 13:44
Ian (on the forums) had a question about how to download email from a POP3 server, process it using his own custom rules, and then resending it.
The trick to resending it, is that he wanted to the original email to be resent and unmodified. This is a little different than the usual request. Usually, developers need to modify the original email in some fashion, rebuild it, and resend it.
In this example, Ian wanted to resend the email, as if it's never been sent. It needs to dynamically determine who to send it to, and forward it on. This makes his application work like an email proxy of sorts.
Below is a code example that would get Ian started on something like this.
As always, if anyone has any questions about this, feel free to use the Contact Us page, to send any questions.
Thanks,
Dave
Here is the code
void Execute()
{
//connect to the pop3 server
POP3 pop = new POP3();
pop.Server = "mail.blah.com";
pop.Username = "dave@blah.com";
pop.Password = "test";
//enable logging
pop.LogPath = "c:\\pop.log";
//login
pop.Login();
//get the first message as text
string content = pop.GetMessageAsText(0);
//process the message
Process( content );
ResendAsOriginal( content );
//if we want to delete the message, uncomment the following lines
//pop.Delete(0);
//pop.CommitDeletes();
//disconnect
pop.Disconnect();
}
void Process( string emailContent )
{
//create the MimeMessage
MimeMessage m = MimeMessage.ParseString( emailContent );
//do whatever we need to with the extracted data
if( m.Subject != null )
{
string subject = m.Subject.Value;
}
}
void ResendAsOriginal( string emailContent )
{
//create the EmailMessage object
EmailMessage msg = new EmailMessage();
//set the server
msg.Server = "192.168.1.106";
//set the TO and FROM values
//these may need to be dynamic, and depend up on the
//value of emailContent
//for this example, they are hard coded
msg.To = "CustomerService@blah.com";
msg.FromAddress = "proxy@myserver.com";
msg.SmtpData = emailContent;
msg.Send();
}