C#+jmail组建接收邮件出现问题,请高手帮忙
我注册了Jmail4.4,意图是将邮件信息写到datagridview控件中,下面是接收邮件的代码://对邮件内容进行Base64编码
public static string Base64Decode(string str)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(str));
}
private void button1_Click(object sender, EventArgs e)
{
pwd = textBox3.Text.ToString();
user = textBox2.Text.ToString();
strserver = textBox1.Text.ToString();
popMail = new jmail.POP3Class();
try
{
popMail.Connect(user, pwd, strserver, 110);
TlabUser.Text = user;
TlabNum.Text = popMail.Count + "封";
if (popMail.Count != 0)
{
dgvEmailInfo.RowCount = popMail.Count;
if (popMail.Count > 0)
{
for (int i = 1; i < popMail.Count + 1; i++)
{
mailMessage = popMail.Messages[i];
if (mailMessage.From == user.Trim().ToLower())//判断邮件发送人是不是自己
{
dgvEmailInfo.Rows[i - 1].Cells[0].Value = "我";
}
else
{
dgvEmailInfo.Rows[i - 1].Cells[0].Value = mailMessage.From;
}
//显示邮件信息
dgvEmailInfo.Rows[i - 1].Cells[1].Value = Base64Decode(mailMessage.Subject);
dgvEmailInfo.Rows[i - 1].Cells[2].Value = Base64Decode(mailMessage.Body);
dgvEmailInfo.Rows[i - 1].Cells[4].Value = mailMessage.Date.ToString();
if (popMail.Count >= 1 && i <= popMail.Count)
{
atts = mailMessage.Attachments;//获取邮件附件
if (atts.Count > 0)
{
dgvEmailInfo.Rows[i - 1].Cells[3].Value = "附件下载";
}
else
{
dgvEmailInfo.Rows[i - 1].Cells[3].Value = "无附件";
}
}
}
}
}
else
{
dgvEmailInfo.Rows.Clear();
}
}
catch
{
MessageBox.Show("该用户邮箱不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
popMail.DeleteSingleMessage(index + 1);
popMail.Disconnect();
MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
button1_Click(sender, e);
}
catch
{
MessageBox.Show("删除失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void dgvEmailInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 3)
{
mailMessage = popMail.Messages[e.RowIndex + 1];//实例化邮件信息对象
atts = mailMessage.Attachments;
for (int k = 0; k < atts.Count; k++)
{
att = atts[k];
string attname = att.Name;//获取附件名
Directory.CreateDirectory("AttachFiles\\" + user);
string mailPath = "AttachFiles\\" + user + "\\" + ReplaceName(att.Name);//设置信息的附件名称
att.SaveToFile(mailPath);
MessageBox.Show("下载成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch
{ }
}
private string ReplaceName(string strName)
{
string strNewName = "";
strNewName = strName.Replace(strName.Substring(0, strName.LastIndexOf(".") + 1), DateTime.Now.ToString("yyyymmddhhmmss") + ".");
return strNewName;
}
}
}
运行正确,但是服务器输入:pop.163.com,用户名:****@163.com,密码:******,后就直接错误输出:该用户名不存在。不知道那里出现了错误,请大家帮忙!谢谢了! --------------------编程问答-------------------- --------------------编程问答-------------------- 请高手帮忙啊! --------------------编程问答-------------------- 请高手指点啊 --------------------编程问答-------------------- 关注~ --------------------编程问答-------------------- 应该是pop3.163.com,我那时做的时候,好像163邮箱对哪个时间段前注册的邮箱是不支持POP3的,你要看一下,大致代码如下,具体实现下载http://download.csdn.net/source/2187303
public void ReceiveMails()
{
jmail.Message Msg = new jmail.Message();
jmail.POP3 jpop = new jmail.POP3();
jmail.Attachments atts;
jmail.Attachment att;
strMailUsername = txtUserName.Text.Trim();
strMailpassword = txtPassword.Text.Trim();
strPOP3Address = txtServerName.Text.Trim();
MailPopPort = Convert.ToInt32(txtMailPort.Text.Trim());
Msg.ISOEncodeHeaders = true;
jpop.Connect(strMailUsername, strMailpassword, strPOP3Address, MailPopPort);
//如果服务器上有邮件
if (jpop.Count >= 1)
{
for (int i = 1; i <= jpop.Count; i++)
{
Msg.Charset = "gb2312";
Msg.ContentTransferEncoding = "base64";
Msg.Encoding = "base64";
Msg.ISOEncodeHeaders = false;
Msg = jpop.Messages[i];
atts = Msg.Attachments;
richTextBox1.Text += Msg.FromName + " " + Msg.Subject + " " + Msg.Body + " " + Msg.Date.ToLongDateString();
//获取附件上传到服务器并且将信息存入数据库
if (atts.Count >= 1)
{
for (int k = 0; k < atts.Count; k++)
{
att = atts[k];//获得附件
att.SaveToFile(@"c:\" + att.Name);
}
}
}
}
} --------------------编程问答-------------------- mark
补充:.NET技术 , C#