用c#的SmtpClient发邮件如何获得邮件发送失败呢

思杰软件 2010-09-07 10:44:14
目前发邮件程序正常,可以发送。
但是不知道是否真的发过去了。如果取得对方邮件的退信呢???
代码如下:
MailMessage MMsg = new MailMessage();
MMsg.Subject = _subjects;
MMsg.From = (new MailAddress(_username));
MMsg.To.Add(new MailAddress(_username));
MMsg.CC.Add("mywaster@sina.com,mywaster@yahoo.cn");
MMsg.IsBodyHtml = true;//这里启用IsBodyHtml是为了支持内容中的Html。
MMsg.BodyEncoding = Encoding.UTF8;//将正文的编码形式设置为UTF8。
MMsg.Body = _bodys;

SmtpClient SClient = new SmtpClient();
SClient.Host = _sendserver;//google的smtp地址
if (_isssl == "1")
{
_bool_isssl = true;
SClient.EnableSsl = true;
}
else
{
_bool_isssl = false;
SClient.EnableSsl = false;
}

SClient.Port = _thisport;//google的smtp端口

SClient.Credentials = new NetworkCredential(_username, _passwords);
//添加附件
if (this.paths.Count != 0)
{
foreach (string path in this.paths)
{
Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
MMsg.Attachments.Add(data);
}
}

try
{
SClient.Send(MMsg);

this.succList.Items.Add(new ListViewItem(new string[] { _ToMail }));
this.str_status.Text = "发送成功(" + this.succList.Items.Count.ToString() + ")";
//MessageBox.Show("报告老板,验证成功,可以发送邮件", "提示");



}
catch (Exception err)
{

MessageBox.Show(err.Message.ToString());
//更新失败列表
this.falseList.Items.Add(new ListViewItem(new string[] { _ToMail }));
this.str_status.Text = "发送失败(" + this.falseList.Items.Count.ToString() + ")";

}

SClient.Send(MMsg); 是发过去了,但不知道对方是否收到,如何实现是否收到、????
高手给个思路,或者给点代码!!!

谢谢
...全文
494 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
porschev 2010-09-08
  • 打赏
  • 举报
回复
不只是你这边代码要写成功

还有收件服务器会不会给你直接过滤掉。。。

jeven_xiao 2010-09-08
  • 打赏
  • 举报
回复
验证用户是否收到?给他个提示 做个反馈?
wuyq11 2010-09-07
  • 打赏
  • 举报
回复
发送成功也可能被过滤
收发邮件查看
wanghuaide 2010-09-07
  • 打赏
  • 举报
回复
关注,我也是用try..catch来实现,如果没异常就发送成功
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Mail; using System.IO; namespace CNPOPSOFT.Controls.Demo { public partial class MailSender : Form { private string filenameok = ""; private string filenameno = ""; private MailMessage message; public MailSender() { InitializeComponent(); } private void buttonSend_Click(object sender, EventArgs e) { string[] ls = System.IO.File.ReadAllLines (this.textBoxTo.Text.Trim(), System.Text.Encoding.Default); if (ls.Length > 0) { string folder = Application.StartupPath + "\\Logs"; if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); filenameok = folder + "\\" + DateTime.Now.ToString ("yyyy_MM_dd HH mm ss") + "_ok.txt"; filenameno = folder + "\\" + DateTime.Now.ToString ("yyyy_MM_dd HH mm ss") + "_no.txt"; for (int a = 0; a < ls.Length; a++) { bool s = false; if (ls[a].ToString().Length < 1 && ls[a].ToString ().IndexOf("@") == -1) continue; else s = Sendemail(ls[a].ToString()); if (s) { FileStream fs1 = new FileStream(filenameok, FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs1); sw.WriteLine("\r\n"); sw.WriteLine ("*******************************"); sw.WriteLine(ls[a].ToString()); sw.WriteLine(DateTime.Now.ToString()); sw.WriteLine ("*******************************"); sw.Close(); fs1.Close(); } else { FileStream fs2 = new FileStream(filenameno, FileMode.Append, FileAccess.Write); StreamWriter sw = new StreamWriter(fs2); sw.WriteLine("\r\n"); sw.WriteLine("******************************"); sw.WriteLine(ls[a].ToString()); sw.WriteLine(DateTime.Now.ToString()); sw.WriteLine("******************************"); sw.Close(); fs2.Close(); } } } if (File.Exists(filenameok)) this.textBox3.Text = filenameok; else this.textBox3.Text = "全部没有发送成功!"; if (File.Exists(filenameno)) this.textBox4.Text = filenameno; else this.textBox4.Text = "全部发送成功!"; } public bool Sendemail(string toemail) { bool flag = false; MailMessage message = BuildMessage(toemail); string host = "smtp." + message.From.Host; int port = 25; string userid = this.textBox1.Text; string password = this.textBox2.Text; SmtpClient smtp = new SmtpClient(host, port); smtp.Credentials = new NetworkCredential(userid, password); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; try { smtp.Send(message); flag = true; //MessageBox.Show("发送成功!", "示例", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { flag = false; //MessageBox.Show("发送失败!\r\n" + ex.Message, "示 例", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return flag; } private MailMessage BuildMessage(string toemail) { string from = this.textBox1.Text; //string to = textBoxTo.Text; string subproject = textBoxSubject.Text; string[] images =null; string body =""; if (this.textBox5.Text.Length < 1) { images = htmlEditor1.Images; body = htmlEditor1.Text; } else { body = System.IO.File.ReadAllText (this.textBox5.Text.ToString(), Encoding.Default); } message = new MailMessage(); message.From = new MailAddress(from); message.To.Add(new MailAddress(toemail)); message.Subject = subproject; message.IsBodyHtml = true; if (images != null) { for (int i = 0, count = images.Length; i < count; ++i) { string image = images[i]; if (image.Trim() == "") { continue; } if (!image.StartsWith("file")) { continue; } string path = Path.GetFullPath(image.Replace("%20", " ").Replace("file:///", "")); string cid = string.Format("image_{0:00}", i); Attachment attach = new Attachment(path); attach.Name = Path.GetFileName(path); attach.ContentId = cid; message.Attachments.Add(attach); body = body.Replace(path, string.Format("cid:{0}", cid));//5+1+a+s+p+x } } message.Body = body; return message; } private void btnadd_Click(object sender, EventArgs e) { if (this.openFileDialog1.ShowDialog() == DialogResult.OK) this.textBoxTo.Text = this.openFileDialog1.FileName; } private void button1_Click(object sender, EventArgs e) { if (this.textBox3.Text.Length > 25) System.Diagnostics.Process.Start(filenameok); } private void button2_Click(object sender, EventArgs e) { if (this.textBox4.Text.Length > 25) System.Diagnostics.Process.Start(filenameno); } private void button3_Click(object sender, EventArgs e) { if (this.openFileDialog2.ShowDialog() == DialogResult.OK) this.textBox5.Text = this.openFileDialog2.FileName; } } }

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧