1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using Jscape.Email;
namespace PopExample
{
class PopExample
{
public Pop myPop = null;
// set default attachment folder
public string attDir = "./messages/attachments/";
public PopExample(string hostname, string username, string password)
{
myPop = new Pop(hostname, username, password);
myPop.LicenseKey = "Email Factory for .NET:Evaluation:Evaluation:01-08-2009:P45IuAOh4AT0k4y93Q7Hjt895yhSB2/667NrPJ2STFam7BTp5UkH97UsMtGe8ZJaSTiD7o+t8PfGjJrsifNMdNViR7rn/gqjqkBl1XqK1a/x0+7eI2Q6sNU7U6Z6hoEOuzBQ4JZo2BJ/aOBfKUfZO+Q0RXMMcJtAOETEKVeLnKI=";
// turn on debug mode
myPop.Debug = true;
// don't delete messages from server
myPop.DeleteMessages = false;
// Subscribe to events
myPop.ConnectedEvent += new Pop.ConnectedEventHandler(OnConnected);
myPop.DisconnectedEvent += new Pop.DisconnectedEventHandler(OnDisconnected);
myPop.DataReceivedEvent += new Pop.DataReceivedEventHandler(OnDataReceived);
myPop.CommandSentEvent += new Pop.CommandSentEventHandler(OnCommandSent);
myPop.MessageRetrievedEvent += new Pop.MessageRetrievedEventHandler(OnMessageRetrieved);
// test current dir structure
if (!Directory.Exists(attDir))
{
Directory.CreateDirectory(attDir);
}
// connect to pop server
myPop.Connect();
int mc = 0;
// retrieve all email messages
IEnumerator e = myPop.GetMessages();
while (e.MoveNext())
{
++mc;
int ac = 0;
EmailMessage message = (EmailMessage)e.Current;
// get attachments for each email, if any
IEnumerator ea = message.GetAttachments();
while (ea.MoveNext())
{
++ac;
Attachment a = (Attachment)ea.Current;
// get name of attached file, if any
String filename = a.GetFilename();
if (filename.Length == 0)
{
// build temporary filename
filename = "att" + mc + "_" + ac + ".txt";
}
// get data for attached file
byte[] data = a.GetFileData();
// save the attachment
FileStream fs = new FileStream(attDir + filename, FileMode.Create, FileAccess.Write);
BinaryWriter w = new BinaryWriter(fs);
w.Write(data, 0, data.Length);
fs.Close();
}
}
// your server may require a slight delay in order to respond.
Thread.Sleep(100);
// disconnect from server
myPop.Disconnect();
}
[STAThread]
static void Main(string[] args)
{
string hostname = "IP/exchange";
string username = "login";
string password = "pwd";
PopExample popexample = new PopExample(hostname, username, password);
}
public void OnConnected(object sender, PopConnectedEventArgs e)
{
Console.WriteLine("Connected to {0}", e.Host);
}
public void OnDisconnected(object sender, PopDisconnectedEventArgs e)
{
if (myPop.IsConnected())
{
myPop.Disconnect();
}
Console.WriteLine("Disconnected.");
}
public void OnDataReceived(object sender, PopDataReceivedEventArgs e)
{
Console.WriteLine("Response: " + e.Response);
}
public void OnCommandSent(object sender, PopCommandSentEventArgs e)
{
Console.WriteLine("Command: " + e.Command);
}
public void OnMessageRetrieved(object sender, PopMessageRetrievedEventArgs e)
{
Console.WriteLine("Message subject= " + e.Message.Subject);
}
}
} |
Partager