C# 使用c将.pst导入mysql时电子邮件地址显示不正确#
我正在使用下面的代码将一个outlook挂载的.pst文件导出到本地MySQL数据库,大部分代码工作正常,但是一些“发件人电子邮件”和“收到的电子邮件”会以EX格式显示,如下所示 /o=ExchangeLabs/ou=Exchange管理组(FYDIBOPDLT)/cn=Recipients/cn=ed403ae50a4581-a.john 这让我发疯了!电子邮件地址应该是a。john@domain.com与这种笨拙的格式不同,其他电子邮件的@domain等格式看起来不错,只是其中一些有这种笨拙的格式-有人知道我需要编辑什么才能使这段代码正常工作吗?我包括了下面的所有代码,因为我是初学者,这段代码没有编写完全靠我自己,所以我非常感谢你能给我的任何建议C# 使用c将.pst导入mysql时电子邮件地址显示不正确#,c#,mysql,mailitem,C#,Mysql,Mailitem,我正在使用下面的代码将一个outlook挂载的.pst文件导出到本地MySQL数据库,大部分代码工作正常,但是一些“发件人电子邮件”和“收到的电子邮件”会以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;
}
}
使用Microsoft.Office.Interop.Outlook;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Threading.Tasks;
使用MySql.Data.MySqlClient;
使用系统配置;
名称空间控制台AP3
{
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
var myconn=新的MySqlConnection
(“服务器=0.0.0.0;”+
“用户=用户;”+
“数据库=sys;”+
“端口=0000;”+
“密码=密码;”+
“连接超时=1;”;
myconn.Open();
列表邮件项=readPst(@“C:\Users\john\Desktop\working.pst”,“working”);
int计数器=0;
int totalMailItemCount=mailItems.Count();
对于(int i=0;i
}这是一个类型为
EX
的完全有效的地址(与SMTP
相反)。您可以使用MailItem.SenderEmailType
属性检查发件人电子邮件地址类型。如果是SMTP
,只需使用MailItem.SenderEmailAddress
。如果不是,请使用MailItem.sender.GetExchangeUser.PrimarySmtpAddress
属性(准备处理错误和/或空值).GetExchangeUser
仅当当前Outlook配置文件中有原始Exchange用户托管有问题的GAL用户时才起作用。如果配置文件中只有PST文件,则不起作用。
很可能是带有MAPI属性(DASL名称
http://schemas.microsoft.com/mapi/proptag/0x5D01001F
)可在邮件中找到-您可以使用MailItem.PropertyAccessor.GetProperty来访问它(单击IMessage按钮)查看邮件若要检查属性是否确实存在请向我们显示该表的创建表
。发件人的电子邮件
是否定义为100个字符长?邮件项目[i]的确切值是多少。发件人邮件地址
(从即时窗口复制,请勿猜测)当您得到不喜欢的值时(如/o=ExchangeLabs/ou=Exchange管理组