我使用下面的代码将一个安装在outlook上的.pst文件导出到一个本地mysql数据库中,大部分代码工作正常,但是一些“sender\u email”和“received\u email”以其ex显示,如下所示
/o=exchangelabs/ou=exchange管理组(fydibopdlt)/cn=recipients/cn=ed403ae50a4581-a.john
我快疯了!电子邮件地址应为a。john@domain.com 而不是这种尴尬的格式,其他电子邮件似乎与@domain等只是其中一些有这种尴尬的格式-有人知道我需要编辑,使这段代码正常工作?我包括以下所有的代码,因为我是一个初学者,这段代码不是完全由我自己写的,所以我将非常感谢任何建议,你可以给我。
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
try
{
var myconn = new MySqlConnection
("server = 0.0.0.0;" +
"user = user;" +
"database = sys;" +
"port = 0000;" +
"password = password;" +
"Connect Timeout=1;");
myconn.Open();
List<MailItem> mailItems = readPst(@"C:\Users\john\Desktop\working.pst", "working");
int counter = 0;
int totalMailItemCount = mailItems.Count();
for (int i = 0; i < mailItems.Count(); ++i)// MailItem mailItem in mailItems)
{
var dateTimeSentToInsert = mailItems[i].SentOn.ToString("yyyy-MM-dd H:mm:ss");
var dateTimeReceivedToInsert = mailItems[i].ReceivedTime.ToString("yyyy-MM-dd H:mm:ss");
var recipients = "";
foreach (Recipient recipient in mailItems[i].Recipients)
{
recipients += recipient.Address + "~";
}
MySqlCommand command = myconn.CreateCommand();
command.CommandText = "INSERT INTO mail2 (Sender_Name, Sender_Email, Received_Name, Received_Email, Date_Sent, Date_Received, Subject, Body" +
") VALUES (@sender_name, @sender_email, @received_name, @received_email, @date_sent, @date_received, @subject, @body)";
command.Parameters.AddWithValue("@sender_name", mailItems[i].SenderName);
command.Parameters.AddWithValue("@sender_email", mailItems[i].SenderEmailAddress);
command.Parameters.AddWithValue("@received_name", mailItems[i].ReceivedByName);
command.Parameters.AddWithValue("@received_email", recipients);
command.Parameters.AddWithValue("@date_sent", dateTimeSentToInsert);
command.Parameters.AddWithValue("@date_received", dateTimeReceivedToInsert);
command.Parameters.AddWithValue("@subject", mailItems[i].Subject);
command.Parameters.AddWithValue("@body", mailItems[i].Body);
command.ExecuteNonQuery();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItems[i]);
Console.WriteLine(++counter + " completed out of " + totalMailItemCount);
}
myconn.Dispose();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
private static List<MailItem> readPst(string pstFilePath, string pstName)
{
List<MailItem> mailItems = new List<MailItem>();
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
// Traverse through all folders in the PST file
// TODO: This is not recursive, refactor
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders)
{
Items items = folder.Items;
foreach (object item in items)
{
if (item is MailItem)
{
MailItem mailItem = item as MailItem;
mailItems.Add(mailItem);
}
}
}
// Remove PST file from Default Profile
//outlookNs.RemoveStore(rootFolder);
return mailItems;
}
}
}
1条答案
按热度按时间eh57zj3b1#
这是一个类型完全有效的地址
EX
(相对于SMTP
). 您可以使用MailItem.SenderEmailType
财产。如果是的话SMTP
,就用mailItem.SenderEmailAddress
. 如果没有,使用MailItem.Sender.GetExchangeUser.PrimarySmtpAddress
属性(准备好处理错误和/或空值)。GetExchangeUser
仅当当前outlook配置文件中有原始exchange用户托管有问题的gal用户时,此选项才起作用。如果配置文件中只有pst文件,它将不起作用。机会是PidTagSenderSmtpAddress_W
mapi属性(dasl名称)http://schemas.microsoft.com/mapi/proptag/0x5D01001F
)在消息中可用-您可以使用MailItem.PropertyAccessor.GetProperty
-使用outlookspy查看消息(单击imessage按钮)以检查属性是否确实存在