使用 C# 将 .pst 导入 mysql 时,电子邮件地址显示不正确
email address not displaying correctly when importing .pst to mysql using c#
我正在使用下面的代码将名为 working 的 outlook 安装的 .pst 文件导出到本地 MySQL 数据库,大部分代码工作正常,但有些 "Sender_Email and "Received_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;
}
}
}
这是一个完全有效的 EX
类型地址(相对于 SMTP
)。您可以使用 MailItem.SenderEmailType
属性 检查发件人电子邮件地址类型。如果是SMTP
,就用MailItem.SenderEmailAddress
。如果不是,请使用 MailItem.Sender.GetExchangeUser.PrimarySmtpAddress
属性(准备好处理错误 and/or 空值)。 GetExchangeUser
仅当原始 Exchange 用户在当前 Outlook 配置文件中托管有问题的 GAL 用户时才有效。如果您的配置文件中只有 PST 文件,它将不起作用。
可能 PidTagSenderSmtpAddress_W
MAPI 属性(DASL 名称 http://schemas.microsoft.com/mapi/proptag/0x5D01001F
)在邮件中可用 - 您可以使用 MailItem.PropertyAccessor.GetProperty
访问它 - 查看带有 [=20= 的邮件](我是它的作者 - 点击 IMessage 按钮)检查 属性 是否确实存在
我正在使用下面的代码将名为 working 的 outlook 安装的 .pst 文件导出到本地 MySQL 数据库,大部分代码工作正常,但有些 "Sender_Email and "Received_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;
}
}
}
这是一个完全有效的 EX
类型地址(相对于 SMTP
)。您可以使用 MailItem.SenderEmailType
属性 检查发件人电子邮件地址类型。如果是SMTP
,就用MailItem.SenderEmailAddress
。如果不是,请使用 MailItem.Sender.GetExchangeUser.PrimarySmtpAddress
属性(准备好处理错误 and/or 空值)。 GetExchangeUser
仅当原始 Exchange 用户在当前 Outlook 配置文件中托管有问题的 GAL 用户时才有效。如果您的配置文件中只有 PST 文件,它将不起作用。
可能 PidTagSenderSmtpAddress_W
MAPI 属性(DASL 名称 http://schemas.microsoft.com/mapi/proptag/0x5D01001F
)在邮件中可用 - 您可以使用 MailItem.PropertyAccessor.GetProperty
访问它 - 查看带有 [=20= 的邮件](我是它的作者 - 点击 IMessage 按钮)检查 属性 是否确实存在