我正在使用 OpenPop 从电子邮件中提取附件并将它们存储在本地驱动器上。
我看到的错误是
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) 在 System.Reflection.RuntimeMethodInfo。在 System.RuntimeType.InvokeMember(字符串名称、BindingFlags bindingFlags、BindingFlags、Object 目标、Object[] providedArgs、ParameterModifier[] 修饰符、CultureInfo 文化中调用(Object obj、BindingFlags invokeAttr、Binder binder、Object[] 参数、CultureInfo 文化) , String[] namedParams) 在 Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
我不确定如何调试此问题。
我正在使用的代码如下...
#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();
}