什麼,我能理解,你有現有的電子郵件某處歸檔服務器上,並且你想將它們導入到Outlook在線或Outlook Office 365的
你能做什麼,你可以利用交易所和Web服務導入您導出的電子郵件。大多數電子郵件可以通過.eml或.msg格式導入。我可以爲您提供.eml文件的指導。
在您的歸檔服務器,你可以獲取郵件的.eml文件備份,也可以通過從Outlook桌面/ Mozilla Thunderbird中導出郵件用於測試目的生成一個。
現在你可以使用NuGet包Microsoft.Exchange.WebServices這實際上是託管API爲Microsoft Exchange Web服務
您可以利用下面的代碼
void main()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
// Get the information of the account
service.Credentials = new WebCredentials("account email here", "account password here");
service.AutodiscoverUrl("account email here", RedirectionUrlValidationCallback);
UploadMIMEEmail(service);
}
public static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation
// callback, the redirection URL is considered valid if it is using HTTPS
// to encrypt the authentication credentials.
if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
private static void UploadMIMEEmail(ExchangeService service)
{
EmailMessage email = new EmailMessage(service);
string emlFileName = @"E:\asad.eml";
using (FileStream fs = new FileStream(emlFileName, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fs.Length];
int numBytesToRead = (int)fs.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fs.Read(bytes, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
// Set the contents of the .eml file to the MimeContent property.
email.MimeContent = new MimeContent("UTF-8", bytes);
}
// Indicate that this email is not a draft. Otherwise, the email will appear as a
// draft to clients.
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
email.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
// This results in a CreateItem call to EWS. The email will be saved in the Inbox folder.
email.Save(WellKnownFolderName.Inbox);
}
什麼這種方法確實是上傳以導入的電子郵件的形式向交換服務器發送電子郵件,其中所有電子郵件數據都與導出的.eml中的完全一致
如果Exchange服務器是域之內本地運行/那麼你可以通過
service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx");
另外如果你想使用默認憑證,然後登錄還可以指定交換URL可以指定
service.UseDefaultCredentials = true;
欲瞭解更多信息,您可以關注
https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx#bk_importproperties
https://code.msdn.microsoft.com/how-to-import-vcard-files-ffa0ff50
啊,會的。同時,提供這種能力的最接近的API是什麼? EWS託管API會訣竅嗎? – lolski
EWS可以做到這一點,特別是如果你有一個MIME流來導入:https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx –