OpenPop Body c#
OpenPop Body c#
我正在尝试从电子邮件中读取正文,但在我的数据网格视图中我只有两条消息,并且有一条信息:"object reference not set to an instance of an object"
private DataTable dt;
private Pop3Client client = new Pop3Client();
private void button11_Click(object sender, EventArgs e)
{
dt = new DataTable("Inbox");
dt.Columns.Add("Subject");
dt.Columns.Add("Sender");
dt.Columns.Add("Body");
dt.Columns.Add("Time");
dataGridView1.DataSource = dt;
try
{
client.Connect("pop-mail.outlook.com", 995, true);
client.Authenticate(textBox6.Text, textBox5.Text);
var count = client.GetMessageCount();
int messageCount = client.GetMessageCount();
for (int i = count; i >= 1; i -= 1)
{
OpenPop.Mime.Message message = client.GetMessage(i);
OpenPop.Mime.MessagePart messagePart = message.MessagePart.MessageParts[1];
string test = messagePart.BodyEncoding.GetString(messagePart.Body);
dt.Rows.Add(new object[] { message.Headers.Subject, message.Headers.Sender, test, message.Headers.DateSent });
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过检查 null
确保您的消息对象不为 null
OpenPop.Mime.Message message = client.GetMessage(i);
//Check you message is not null
if(message != null){
OpenPop.Mime.MessagePart messagePart = message.MessagePart.MessageParts[1];
string test = messagePart.BodyEncoding.GetString(messagePart.Body);
dt.Rows.Add(new object[] { message.Headers.Subject, message.Headers.Sender, test, message.Headers.DateSent });
}
我正在尝试从电子邮件中读取正文,但在我的数据网格视图中我只有两条消息,并且有一条信息:"object reference not set to an instance of an object"
private DataTable dt;
private Pop3Client client = new Pop3Client();
private void button11_Click(object sender, EventArgs e)
{
dt = new DataTable("Inbox");
dt.Columns.Add("Subject");
dt.Columns.Add("Sender");
dt.Columns.Add("Body");
dt.Columns.Add("Time");
dataGridView1.DataSource = dt;
try
{
client.Connect("pop-mail.outlook.com", 995, true);
client.Authenticate(textBox6.Text, textBox5.Text);
var count = client.GetMessageCount();
int messageCount = client.GetMessageCount();
for (int i = count; i >= 1; i -= 1)
{
OpenPop.Mime.Message message = client.GetMessage(i);
OpenPop.Mime.MessagePart messagePart = message.MessagePart.MessageParts[1];
string test = messagePart.BodyEncoding.GetString(messagePart.Body);
dt.Rows.Add(new object[] { message.Headers.Subject, message.Headers.Sender, test, message.Headers.DateSent });
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
通过检查 null
确保您的消息对象不为 nullOpenPop.Mime.Message message = client.GetMessage(i);
//Check you message is not null
if(message != null){
OpenPop.Mime.MessagePart messagePart = message.MessagePart.MessageParts[1];
string test = messagePart.BodyEncoding.GetString(messagePart.Body);
dt.Rows.Add(new object[] { message.Headers.Subject, message.Headers.Sender, test, message.Headers.DateSent });
}