C# 中用System.Net.Mail.MailMessage来发邮件出错的问题

zzmdegm 2008-06-05 02:06:00
我的代码如下:
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.163.com";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@163.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@163.com", "zzmdegm@yahoo.com.cn");
message.Subject = "测试";
message.Body = "用自己写的软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);

try
{
client.Send(message);
}
catch (Exception ex)
{}

运行的时候老是报错:{"邮箱不可用。 服务器响应为: Óû§±»Ëø¶¨"}
这是什么原因啊
...全文
6543 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzmdegm 2008-06-30
  • 打赏
  • 举报
回复
用这一个吧,大多数是能发送:
using System.Net.Mail;
/// <summary>
/// 使用Gmail邮箱发送邮件
/// </summary>
/// <param name="Sender">发信人</param>
/// <param name="SenderPassword">发信人密码</param>
/// <param name="Addressee">收信人</param>
/// <param name="maildisplayName">邮件名</param>
/// <param name="mailSubject">邮件主题</param>
/// <param name="mailBody">邮件内容</param>
/// <returns>返回空为发送成功,不为空是错误描述</returns>
public static string SendMailByGmail(string Sender, string SenderPassword, string Addressee, string maildisplayName, string mailSubject, string mailBody)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential(Sender, SenderPassword);
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.EnableSsl = true;
mail = new MailMessage();
String[] addr = Addressee.Split(',');
try
{
mail.From = new MailAddress(Sender, maildisplayName, System.Text.Encoding.UTF8);
Byte i;
for (i = 0; i < addr.Length; i++)
mail.To.Add(addr[i]);
mail.Subject = mailSubject;
mail.Body = mailBody;
//if(ListBox1.Items.Count != 0)
//{
// for(i = 0;i<ListBox1.Items.Count;i++)
// mail.Attachments.Add(new Attachment(ListBox1.Items[i].ToString()));
//}

mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress(Addressee);
SmtpServer.Send(mail);
//Response.Write("<script>alert('发送成功!');</script>");
return "";
}
catch (Exception ex)
{
//Response.Write(ex.ToString());
return ex.ToString();
}
}


调用:
    protected void Button1_Click(object sender, EventArgs e)
{
//SendMail _sm = new SendMail();
string sendExstr = SendMail.SendMailByGmail("zzmdegm@gmail.com", "******", this.TxtB_Addressee.Text.Trim(), "XX公司", "XX公司-用户密码找回!", "616818!");
if (sendExstr == "")
{ Response.Write("<script>alert('发送成功!');</script>"); }
else
{ Response.Write(sendExstr); }
}
zzmdegm 2008-06-09
  • 打赏
  • 举报
回复
to:weiphone
我的调用:string _sendMessage=SendEMail("zzmdegm@sina.com", "123456", "网站密码传递", this.TxtB_UserE_mail.Text.Trim(), null, "密码", null, DateTime.Now.ToString());

用你的代码,总是第一次发的时候很正常,都可以收得到,但第二次发就出现这样的错误;
System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.IO.IOException: 无法从传输连接中读取数据: net_io_connectionclosed。 在 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) 在 System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 在 System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 在 System.Net.Mail.SmtpReplyReader.ReadLine() 在 System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) 在 System.Net.Mail.DataStopCommand.Send(SmtpConnection conn) 在 System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args) 在 System.Net.ClosableStream.Close() 在 System.Net.Mail.MailWriter.Close() 在 System.Net.Mail.SmtpClient.Send(MailMessage message) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.Mail.SmtpClient.Send(MailMessage message) 在 sendpass.SendEMail(String from, String password, String DisPalyName, String to, String cc, String subject, Boolean ssl, MailPriority priority, Boolean isbodyhtml, Attachment attachment, String body) 位置 d:\My Documents\Visual Studio 2005\WebSites\cp08\sendpass.aspx.cs:行号 357
zzmdegm 2008-06-05
  • 打赏
  • 举报
回复
晕s
把收件地址改为163的就可以正常收到了,这是什么原因啊!
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@sina.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@sina.com", "zzmdegm@163.com");
message.Subject = "测试aa";
message.Body = "用自己写的asdfasdf软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);

try
{
client.Send(message);
}
catch (Exception ex)
{ }
weiphone 2008-06-05
  • 打赏
  • 举报
回复
我的用163也可以的
自己看看

#region 2、电子邮件

/// <summary>
/// 发送电子邮件方法
/// </summary>
/// <param name="from">发件人地址</param>
/// <param name="password">密码</param>
/// <param name="DisPalyName">对方收到时显示的名称</param>
/// <param name="to">收件人</param>
/// <param name="cc">抄送:默认为空</param>
/// <param name="subject">主题</param>
/// <param name="ssl">经过ssl加密</param>
/// <param name="priority">邮件优先级:MailPriority.High</param>
/// <param name="isbodyhtml">是否HTML邮件</param>
/// <param name="attachment">附件:没有则为null</param>
/// <param name="body">正文</param>
/// <returns>返回值:如果为空则成功,否则失败</returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, bool ssl, MailPriority priority, bool isbodyhtml, Attachment attachment, string body)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress(from, DisPalyName, Encoding.UTF8);
mm.To.Add(to);
mm.SubjectEncoding = Encoding.UTF8;
mm.Subject = subject;
if (attachment != null)
mm.Attachments.Add(attachment);
if (cc != null && cc != "")
mm.CC.Add(cc); //抄送
mm.IsBodyHtml = isbodyhtml; //是否是HTML邮件
mm.BodyEncoding = Encoding.UTF8; //编码
mm.Priority = priority; // MailPriority.High; //优先级
mm.Body = body;
SmtpClient sc = new SmtpClient();
sc.Credentials = new System.Net.NetworkCredential(from, password);
//sc.Port = 25;
sc.Host = "smtp." + from.Substring(from.IndexOf('@') + 1);
try
{
sc.Send(mm);
return "";
}
catch (Exception ee)
{
return ee.ToString();
}
}

/// <summary>
/// 发送电子邮件方法
/// </summary>
/// <param name="from">发件人地址</param>
/// <param name="password">密码</param>
/// <param name="DisPalyName">对方收到时显示的名称</param>
/// <param name="to">收件人</param>
/// <param name="cc">抄送:默认为空</param>
/// <param name="subject">主题</param>
/// <param name="attachment">附件</param>
/// <param name="body">正文</param>
/// <returns>返回值:如果为空则成功,否则失败</returns>
public static string SendEMail(string from, string password, string DisPalyName, string to, string cc, string subject, Attachment attachment, string body)
{
return SendEMail(from, password, DisPalyName, to, cc, subject, false, MailPriority.High, false, attachment, body);
}


/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址</param>
/// /// <param name="MaxSize">最大容量,单位:M</param>
/// <returns></returns>
public static Attachment GetAttachment(string file, int MaxSize)
{
if (!File.Exists(file))
{
System.Web.HttpContext.Current.Response.Write("<script>alert('" + file + "文件不存在!')</script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
int size = 0;
FileStream fs = new FileStream(file, FileMode.Open);
string name = fs.Name;
size = (int)(fs.Length / 1024);
fs.Close();

//控制文件大小不大于10M
if (size > 1024 * MaxSize)
{
System.Web.HttpContext.Current.Response.Write("<script>alert('附件容量过大!')</script>");
System.Web.HttpContext.Current.Response.End();
return null;
}
Attachment att = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = att.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
return att;
}

/// <summary>
/// 判断附件容量
/// </summary>
/// <param name="file">文件地址</param>
/// <returns></returns>
public static Attachment GetAttachment(string file)
{
return GetAttachment(file, 10);
}
#endregion

自己调用
weiphone 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ericzhangbo1982111 的回复:]
网易邮件系统维护公告 打印本文  2006-11-15

亲爱的用户朋友:

自2006年11月16日起,网易免费邮件系统的POP3及SMTP服务将暂停开通,此前已经开通POP3及SMTP服务的邮箱帐号则不受影响,仍然可以正常使用。

对此如有任何疑问, 请通过网易在线客服提交你的问题。

网易邮件中心
2006年11月15日


用sina的邮箱看看吧
[/Quote]

我都可以用的
zzmdegm 2008-06-05
  • 打赏
  • 举报
回复
把它换成sina的:
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("zzmdegm@sina.com", "123456");
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("zzmdegm@sina.com", "zzmdegm@yahoo.com.cn");
message.Subject = "测试";
message.Body = "用自己写的软件发的邮件!";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
////添加附件
//Attachment data = new Attachment(@"附件地址如:e:\a.jpg", System.Net.Mime.MediaTypeNames.Application.Octet);
//message.Attachments.Add(data);

try
{
client.Send(message);
}
catch (Exception ex)
{ }

代码运行通过,但yahoo里面就是收不到,晕s
zzmdegm 2008-06-05
  • 打赏
  • 举报
回复
把防火关了,还是不行
angelababa~ 2008-06-05
  • 打赏
  • 举报
回复
不过跟防火墙好像有点关系
angelababa~ 2008-06-05
  • 打赏
  • 举报
回复
单封的发不太累了吗,还是一群一群的发爽啊,嘎嘎
代码没问题的哦
angelababa~ 2008-06-05
  • 打赏
  • 举报
回复

//群发邮件
private void SendMyMail()
{
string Accessory = ""; //附件
string SendTos = "";  //发送人
string CcTos = "";   //多个抄送人用逗号隔开,实现群发
string[] Receiver = System.Text.RegularExpressions.Regex.Split(CcTos, ","); //总收件人
string FromMail = ""; //发件人
string Title = ""; //邮件主题
string Body = ""; //邮件主体
string ShowName=""; //显示名字
string Host = "smtp.163.com"; //邮件服务器 ,如果使用其他邮件服务器一定要改这地方,根据发送邮箱而不同
string EmailPwd = ""; //发件人邮箱密码
if (SendTos!=""||CcTos!="")
{
//用户名(取发送邮箱的@前面的部分,即ustbwuyi1
string MailUserName = FromMail.Substring(0, FromMail.IndexOf("@"));

//密码
string MailPassword = EmailPwd;//发件人邮箱密码
MailMessage mailmessage = new MailMessage();
MailAddress n = new MailAddress(FromMail);
mailmessage.From = n;
mailmessage.Subject = Title;
mailmessage.IsBodyHtml = true;
mailmessage.Body = Body;

//添加附件

ArrayList FileNames = new ArrayList();
FileNames.Add(@Accessory);//附件地址
//如果存在附件
if (FileNames.Count>= 1)
{
//添加附件
string Filename;
for (int k = 0; k < FileNames.Count; k++)
{
Filename = FileNames[k].ToString();
mailmessage.Attachments.Add(new Attachment(Filename));
}
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = Host;
client.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
//向收件人和抄送人循环发送
for (int j = 0; j < Receiver.Length; j++)
{
mailmessage.To.Add(Receiver[j].ToString());
try
{
client.Send(mailmessage);
Response.Write(" <script language='javascript'> alert('用户:" + Receiver[j] + "邮件发送成功') </script> ");
//System.Web.Mail.SmtpMail.Send(mailmessage);
}
catch (Exception ex)
{
Response.Write(" <script language='javascript'> alert('用户:"+Receiver[j]+"邮件发送失败:" + ex.Message + "') </script> ");
}
}
}
}

ericzhangbo1982111 2008-06-05
  • 打赏
  • 举报
回复
把防火墙管关了看看
或者你在公司的网络用的是代理服务器
贫僧又回来了 2008-06-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ericzhangbo1982111 的回复:]
网易邮件系统维护公告 打印本文  2006-11-15

亲爱的用户朋友:

自2006年11月16日起,网易免费邮件系统的POP3及SMTP服务将暂停开通,此前已经开通POP3及SMTP服务的邮箱帐号则不受影响,仍然可以正常使用。

对此如有任何疑问, 请通过网易在线客服提交你的问题。

网易邮件中心
2006年11月15日


用sina的邮箱看看吧
[/Quote]
好象昨天还有人用163的发邮件的啊!
参看http://topic.csdn.net/u/20080603/15/e5d9d172-117b-4d92-9345-2599c09d114c.html
ericzhangbo1982111 2008-06-05
  • 打赏
  • 举报
回复
网易邮件系统维护公告 打印本文  2006-11-15

亲爱的用户朋友:

自2006年11月16日起,网易免费邮件系统的POP3及SMTP服务将暂停开通,此前已经开通POP3及SMTP服务的邮箱帐号则不受影响,仍然可以正常使用。

对此如有任何疑问, 请通过网易在线客服提交你的问题。

网易邮件中心
2006年11月15日


用sina的邮箱看看吧

110,546

社区成员

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

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

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