0
我已經構建了一個從某個文件夾讀取CSV的SSIS包。但是現在我需要從exchange server下載相同的csv。我的機器上也沒有安裝Outlook。我能從交換服務器下載CSV文件嗎?謝謝!使用部署在另一服務器上的SSIS包從Exchange Server下載附件
我已經構建了一個從某個文件夾讀取CSV的SSIS包。但是現在我需要從exchange server下載相同的csv。我的機器上也沒有安裝Outlook。我能從交換服務器下載CSV文件嗎?謝謝!使用部署在另一服務器上的SSIS包從Exchange Server下載附件
我已經使用了鏈接http://sqlandbilearning.blogspot.com.au/2014/07/download-email-attachment-using-ssis.html中的一些代碼,但我已經添加了一些新的代碼,用於使用ServicePointManager除去TCP綁定錯誤以及爲檢索特定電子郵件添加搜索過濾器,並且此代碼還處理來自不同的多個附件將電子郵件保存在文件系統上。
public void Main()
{
string filePath = "";
string fileName = "";
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
DateTime now = DateTime.Now;
DateTime beginRecievedTime = new DateTime(now.Year, now.Month, now.Day, 7, 55, 0);
DateTime finishRecievedTime = new DateTime(now.Year, now.Month, now.Day, 8, 15, 0);
EmailMessage latestEmail = null;
try
{
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.UseDefaultCredentials = true;
//service.Credentials = new WebCredentials("username", "password");
service.Url = new Uri("");
// 10 mails per page in DESC order
ItemView view = new ItemView(10);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
searchFilterCollection.Add(new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Scheduled search"));
SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, beginRecievedTime);
searchFilterCollection.Add(greaterthanfilter);
SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, finishRecievedTime);
searchFilterCollection.Add(lessthanfilter);
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
//Find mails
FindItemsResults<Item> fir = service.FindItems(WellKnownFolderName.Inbox, filter, view);
Dictionary<EmailMessage, string> emailsMap = new Dictionary<EmailMessage, string>();
foreach (Item item in fir.Items)
{
item.Load(); //Load the entire message with attachment
EmailMessage email = item as EmailMessage;
if (email != null)
{
if (email.HasAttachments == true && email.Attachments.Count == 1)
{
if (email.Subject.StartsWith("Scheduled search") == true)
{
filePath = Path.Combine(Dts.Variables["User::SourceFolderPath"].Value.ToString()
, email.DateTimeReceived.Date.ToString("MM.dd.yyyy") + "_" +
email.Attachments[0].Name);
// fileName = email.DateTimeReceived.Date.ToString("MM.dd.yyyy") + "_" +
// email.Attachments[0].Name.ToString();
emailsMap.Add(email, filePath);
}
}
}
}
if (emailsMap.Count > 0) {
foreach (var item in emailsMap) {
//Save attachment
EmailMessage email = item.Key;
filePath = item.Value;
FileAttachment fileAttachment = email.Attachments[0] as FileAttachment;
fileAttachment.Load(filePath);
string extractPath = Dts.Variables["User::SourceFolderPath"].Value.ToString() + "\\" + email.Attachments[0].Name;
System.IO.Compression.ZipFile.ExtractToDirectory(filePath, extractPath);
fileName = Dts.Variables["User::SourceFolderPath"].Value.ToString() + "\\" + email.DateTimeReceived.Date.ToString("MM.dd.yyyy") + "_" +
email.Attachments[0].Name.ToString();
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
}
// Dts.Variables["User::SourceFileName"].Value = fileName;
Dts.TaskResult = (int)ScriptResults.Success;
}
catch(System.Runtime.InteropServices.COMException ex)
{
if (Dts.Variables.Locked == true)
{
Dts.Variables.Unlock();
}
//An error occurred.
Dts.Events.FireError(0, "Error occured", ex.Message, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
您需要一個步驟(它可以是SSIS中的腳本任務,或者它可以是EXE文件或Powershell或其他),它首先下載文件。這是我在google搜索時發現的第一個鏈接:http://sqlandbilearning.blogspot.com.au/2014/07/download-email-attachment-using-ssis.html –
感謝這很有幫助。我發現另一個鏈接https://blog.kloud.com.au/2016/04/19/programmatically-read-email-from-an-exchange-sever-mailbox/,但你的鏈接指向SSIS,所以我會通過腳本任務方式。 –
我通常建議你在SSIS的外部進行操作,因爲我已經遇到了腳本編輯器的問題,並且可能很難調試,因此我建議你在任何適合的平臺上執行它。 –