还是报错,代码如下: //创建smtpclient对象 System.Net.Mail.SmtpClient client = new SmtpClient(); client.Host = "smtp.sina.com.cn"; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("[email protected]", "123456"); client.Timeout = 30000000; client.DeliveryMethod = SmtpDeliveryMethod.Network; //创建mailMessage对象 System.Net.Mail.MailMessage message = new MailMessage("[email protected]", "[email protected]"); 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
System.IndexOutOfRangeException: Index was outside the bounds of the array.
估计是字节流缓冲区满了
楼主不妨试试大附件拆分成若干个小附件,分别发送
然后在接收端拼起来
LZ可以试一下.
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "123456");
client.Timeout=100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage("[email protected]", "[email protected]");
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();
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.sina.com.cn";
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "123456");
client.Timeout = 30000000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage("[email protected]", "[email protected]");
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