I'm using OpenPop to extract attachments from emails and store them on a local drive.
The error I am seeing is
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
I'm not really sure how to debug this issue.
The code I am using is as follows...
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.IO;
using OpenPop.Mime;
using OpenPop.Mime.Header;
using OpenPop.Pop3;
using OpenPop.Pop3.Exceptions;
using OpenPop.Common.Logging;
using System.Linq;
#endregion
//.......
var client = new Pop3Client();
try
{
client.Connect("outlook.office365.com", port, true); //UseSSL true or false
client.Authenticate("user", "password");
var messageCount = client.GetMessageCount();
var Messages = new System.Collections.Generic.List<Message>(messageCount);
for (int i = 0; i < messageCount; i++)
{
Messages.Add(client.GetMessage(i + 1));
}
foreach (Message msg in Messages)
{
foreach (var attachment in msg.FindAllAttachments())
{
string filePath = Path.Combine(@"D:\validations\Attachments", attachment.FileName);
if (Path.GetExtension(filePath) == ".xlsx")//Path.GetExtension(filePath) == ".xlsx" attachment.FileName.Equals("blabla.pdf")
{
FileStream Stream = new FileStream(filePath, FileMode.Create);
BinaryWriter BinaryStream = new BinaryWriter(Stream);
BinaryStream.Write(attachment.Body);
BinaryStream.Close();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("", ex.Message);
}
finally
{
if (client.Connected)
client.Dispose();
}