SmtPop.net is a C# class library that enable your code to deal with SMTP and POP3 servers.
Features :
The MimeMessage class support
Sending a simple e-mail with SmtPop
// build the message
MimeMessage msg = new MimeMessage ();
string body = "This is a simple message for test";
string subject = "A simple message for test";
// build addresses list
msg.AddressFrom.Add (new MailAddress ("toto <toto@toto.com>"));
msg.AddressTo.Add (new MailAddress (TestConstant.toadr));
// Save To and From header
msg.SaveAdr ();
// build subject and body
msg.SetSubject (subject, MimeTransferEncoding.Ascii7Bit);
msg.SetBody (body, MimeTransferEncoding.Ascii7Bit, MimeTextContentType.TextPlain);
// send the message
SMTPClient smtp = new SMTPClient (TestConstant.host, TestConstant.portsmtp);
smtp.Open ();
smtp.SendMail (msg);
smtp.Close ();
// connect to pop server
SmtPop.POP3Client pop = new SmtPop.POP3Client ();
pop.Open ("localhost", "110", "mylogin", "mypasword");
// get messages list from pop server
SmtPop.POPMessageId[] messages = pop.GetMailList ();
if (messages != null)
{
// Walk attachment list
foreach (SmtPop.POPMessageId id in messages)
{
SmtPop.POPReader reader = pop.GetMailReader (id.Id);
SmtPop.MimeMessage msg = new SmtPop.MimeMessage ();
// read message
msg.Read (reader);
if (msg.Attachments != null)
{
// do something with first attachment
SmtPop.MimeAttachment attach = msg.Attachments[0];
if (attach.Filename == "data")
{
// read data from attachment
Byte[] b = Convert.FromBase64String (attach.Body);
System.IO.MemoryStream mem = new System.IO.MemoryStream (b, false);
BinaryFormatter f = new BinaryFormatter ();
DataClass data = (DataClass) f.Deserialize (mem);
mem.Close();
}
//delete message
pop.Dele (id.Id);
}
}
}
}
pop.Quit ();
In the "demo" directory, you will find some samples using smptpop library:
PopSrvToEml | Download e-mails from a POP3 server and write .eml files |
ShowAttachmentList | Load a .eml file and display message headers and structure |
Redirect | Download e-mails from a POP3 server and send them to an SMTP server |
SenderReceiver | Demonstrate object serialisation/deserialisation in e-mail attachment |
DisplayFrom | Extract the "from" address from a list of .eml files |
DisplaySubject | Extract the subject from a list of .eml files |
SmtpSend | Send e-mail from the command line |
If you want to run unit test, you will need a mail server and you need to change some constant in source file "testconstants.cs" (server ip, from and to mailboxes, passwords ...). I recommend to install a mail server on your localhost and configure some anonymous mailboxes (test@bidule.com, longtest@bidule.com) . Currently i'm using James 2.0 and HMailServer 4.1.
Currently i'm using:
Nunit 2.2 as test framework.
Nant 0.85rc3 and Nant Contrib 0.85rc3 as build tool
NDoc 1.3 as documentation generator