发送邮件时,为什么不能发送大的附件

Taotesea 2011-09-05 04:17:45
小的附件代码可以通过,但是大的附件(约10M),就报错
以下是邮件代码:
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("taotetmp@sina.com", "123456");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage("taotetmp@sina.com", "taotetmp@sina.com");
message.Subject = "Test";
//正文默认格式为html
message.Body = "Test";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
Attachment data = new Attachment("C:\\aa.7z", System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
client.Send(message);



以下是报错信息:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Net.Base64Stream.EncodeBytes(Byte[] buffer, Int32 offset, Int32 count, Boolean dontDeferFinalBytes, Boolean shouldAppendSpaceToCRLF)
at System.Net.Base64Stream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mime.MimePart.Send(BaseWriter writer)
at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Test.MainForm.Button1_Click(Object sender, EventArgs e) in d:\SharpDevelop\MyProjects\CSharp\Test\Test\MainForm.cs:line 50
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Test.Program.Main(String[] args) in d:\SharpDevelop\MyProjects\CSharp\Test\Test\Program.cs:line 26
...全文
734 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
i烤麸 2012-03-03
  • 打赏
  • 举报
回复
应该是新浪的邮箱不支持那么大的附件,可以用qq的邮箱
Taotesea 2011-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 skyparty 的回复:]
从异常看是数组越界了
System.IndexOutOfRangeException: Index was outside the bounds of the array.
估计是字节流缓冲区满了
楼主不妨试试大附件拆分成若干个小附件,分别发送
然后在接收端拼起来
[/Quote]
有没有别的方法,不用分成若干个小附件,谢谢:)
「已注销」 2011-09-07
  • 打赏
  • 举报
回复
我用的是163.com 网易的邮箱,发送到我的qq.com 邮箱上的.测试是成功的.你可以弄个163试试.也许你的邮件服务器不支持传送这么大的文件.
山东蓝鸟贵薪 2011-09-06
  • 打赏
  • 举报
回复
不应该呀,可能是文件名有问题吧
shadan05 2011-09-06
  • 打赏
  • 举报
回复
或者你可以把时间设置的更长一点,网络有差异不知道会不会影响? 先搞清楚是什么问题。
Taotesea 2011-09-06
  • 打赏
  • 举报
回复
自己顶起来
Taotesea 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 joxbinyu 的回复:]
数据量过大肯定会有一个TimeOut 问题,你把TimeOut 时间设长一点.等待文件传送完毕.我刚刚试了一下.可以.因为上传10M的文件不是那么快的.而默认的TimeOut 会判断传送失败引发异常.
[/Quote]
还是报错,代码如下:
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("taotetmp@sina.com", "123456");
client.Timeout = 30000000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage("taotetmp@sina.com", "taotetmp@sina.com");
message.Subject = "Test";
//正文默认格式为html
message.Body = "Test";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
Attachment data = new Attachment("C:\\aa.7z", System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
client.Send(message);
Close();


报错如下:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Net.Base64Stream.EncodeBytes(Byte[] buffer, Int32 offset, Int32 count, Boolean dontDeferFinalBytes, Boolean shouldAppendSpaceToCRLF)
at System.Net.Base64Stream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Mime.MimePart.Send(BaseWriter writer)
at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Test.MainForm.Button1_Click(Object sender, EventArgs e) in d:\SharpDevelop\MyProjects\CSharp\Test\Test\MainForm.cs:line 51
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Test.Program.Main(String[] args) in d:\SharpDevelop\MyProjects\CSharp\Test\Test\Program.cs:line 26
Taotesea 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 skyparty 的回复:]
从异常看是数组越界了
System.IndexOutOfRangeException: Index was outside the bounds of the array.
估计是字节流缓冲区满了
楼主不妨试试大附件拆分成若干个小附件,分别发送
然后在接收端拼起来
[/Quote]
还是报错,以下是加入了TimeOut
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("taotetmp@sina.com", "123456");
client.Timeout=100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage("taotetmp@sina.com", "taotetmp@sina.com");
message.Subject = "Test";
//正文默认格式为html
message.Body = "Test";
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8;
Attachment data = new Attachment("C:\\aa.7z", System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
client.Send(message);
Close();
「已注销」 2011-09-05
  • 打赏
  • 举报
回复
我刚刚用你的代码,我把 client.Timeout = 30000000; 发了一个11M的文件发送成功了. 大概等了十几分钟.
LZ可以试一下.
「已注销」 2011-09-05
  • 打赏
  • 举报
回复
数据量过大肯定会有一个TimeOut 问题,你把TimeOut 时间设长一点.等待文件传送完毕.我刚刚试了一下.可以.因为上传10M的文件不是那么快的.而默认的TimeOut 会判断传送失败引发异常.
小赖赖 2011-09-05
  • 打赏
  • 举报
回复
从异常看是数组越界了
System.IndexOutOfRangeException: Index was outside the bounds of the array.
估计是字节流缓冲区满了
楼主不妨试试大附件拆分成若干个小附件,分别发送
然后在接收端拼起来

110,534

社区成员

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

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

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