Ask a questionAsk a question
 

AnswerForward mails via POP3->SMTP

  • Friday, May 25, 2007 4:03 PMalberich Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I need to manage a mail account programmatically. I'll get the mails by POP3, work some RegEx on them and - depending on the RegEx results - want to forward some of them to different email addresses.

    My problem is: By POP3 i'll get the raw mail as ascii string, with quotet-printable text and base64 attachments within.This is ok for the RegEx stuff, but i cant forward this "raw" format. By now, i split the raw mail string at the first empty line, replace the "to"-header, copy all headers into my MailMessage, put the body part of the string into MailMessage.Body and send it using the SmtpClient class.

    This works, but this way the mails are forwarded as raw text mails.
    As i dont like to extract the different contents from the raw body to re-create it using MailMessage.Attachments & co, is there a easy way? Actually i just need to modify some headers (To, Receive, ...) and the ASCII string can go its way...

    thx
    alberich

Answers

  • Friday, May 25, 2007 8:10 PMMartin Vobr from Rebex Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The problem is that you are setting whole original message (including attachments, and MIME encoded parts) into the MailMessage text body which destroys formatting and attachments.

    There are at least following solutions:

    1)  Forget the SmtpClient class and use socket to connect to the SMTP server. The communication (defined in RFC 821) is quite simple:
    S: MAIL FROM:<Smith@Alpha.ARPA>
    R: 250 OK

    S: RCPT TO:<Jones@Beta.ARPA>
    R: 250 OK

    S: RCPT TO:<Green@Beta.ARPA>
    R: 550 No such user here

    S: RCPT TO:<Brown@Beta.ARPA>
    R: 250 OK

    S: DATA
    R: 354 Start mail input; end with <CRLF>.<CRLF>
    S: Blah blah blah...
    S: ...etc. etc. etc.
    S: <CRLF>.<CRLF>
    R: 250 OK
    Paste the downloaded message body after the DATA command. If you don't need things like SMTP authentication, SMTP over TLS/SSL or such it could be enough. It's not so hard considering you already have code for downloading the message from POP3.

    2) Use third party mail component which can construct/load MailMessage from stream or byte buffer and than send it via the SMTP. Or which can download the message from POP3, parse it, let you change the properties and resend it via the SMTP.

    using Rebex.Net;
    using Rebex.Mail;
    ...

    // create client and connect 
    Pop3 pop3client = new Pop3();
    pop3client.Connect("pop3.example.org");

    // authenticate
    pop3client.Login("username", "password");

    // get the first message from the mailbox
    MailMessage message = pop3client.GetMailMessage(1);

    pop3client.Disconnect();

    // do some processing
    message.To = "new@example.com";

    // send the message
    Smtp.Send (message, "smtp.example.org");


    The code above was glued together from various tutorials from
    http://www.rebex.net/mail.net/tutorial-smtp.aspx
    and
    http://www.rebex.net/mail.net/tutorial-pop3.aspx

    Martin

All Replies

  • Friday, May 25, 2007 8:10 PMMartin Vobr from Rebex Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The problem is that you are setting whole original message (including attachments, and MIME encoded parts) into the MailMessage text body which destroys formatting and attachments.

    There are at least following solutions:

    1)  Forget the SmtpClient class and use socket to connect to the SMTP server. The communication (defined in RFC 821) is quite simple:
    S: MAIL FROM:<Smith@Alpha.ARPA>
    R: 250 OK

    S: RCPT TO:<Jones@Beta.ARPA>
    R: 250 OK

    S: RCPT TO:<Green@Beta.ARPA>
    R: 550 No such user here

    S: RCPT TO:<Brown@Beta.ARPA>
    R: 250 OK

    S: DATA
    R: 354 Start mail input; end with <CRLF>.<CRLF>
    S: Blah blah blah...
    S: ...etc. etc. etc.
    S: <CRLF>.<CRLF>
    R: 250 OK
    Paste the downloaded message body after the DATA command. If you don't need things like SMTP authentication, SMTP over TLS/SSL or such it could be enough. It's not so hard considering you already have code for downloading the message from POP3.

    2) Use third party mail component which can construct/load MailMessage from stream or byte buffer and than send it via the SMTP. Or which can download the message from POP3, parse it, let you change the properties and resend it via the SMTP.

    using Rebex.Net;
    using Rebex.Mail;
    ...

    // create client and connect 
    Pop3 pop3client = new Pop3();
    pop3client.Connect("pop3.example.org");

    // authenticate
    pop3client.Login("username", "password");

    // get the first message from the mailbox
    MailMessage message = pop3client.GetMailMessage(1);

    pop3client.Disconnect();

    // do some processing
    message.To = "new@example.com";

    // send the message
    Smtp.Send (message, "smtp.example.org");


    The code above was glued together from various tutorials from
    http://www.rebex.net/mail.net/tutorial-smtp.aspx
    and
    http://www.rebex.net/mail.net/tutorial-pop3.aspx

    Martin
  • Tuesday, June 05, 2007 3:35 PMalberich Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks Martin,
    i solved it now using sockets. You're right, SMTP is not so hard.

    Although i dont have re-sending etc. by now, its working good.

    For anyone with similar problems, here is a way to get the MX record: Search for "C# .NET DNS query component" at codeproject.com

    Thanks again
    alberich
  • Tuesday, November 03, 2009 4:44 PMJohn Borders Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can also try this .NET SMTP Component