private bool SendEmail()
{
//连接网络
try
{
tc=new TcpClient(mailserver,mailserverport);
}
catch(Exception e)
{
errmsg=e.ToString();
return false;
} ns = tc.GetStream();
SMTPCodeAdd(); //验证网络连接是否正确
if(RightCodeHT[RecvResponse().Substring(0,3)]==null)
{
errmsg="网络连接失败";
return false;
}
string[] SendBuffer;
string SendBufferstr; //进行SMTP验证
if(ESmtp)
{
SendBuffer=new String[4];
SendBuffer[0]="EHLO " + mailserver + enter;
SendBuffer[1]="AUTH LOGIN" + enter;
SendBuffer[2]=Base64Encode(username) + enter;
SendBuffer[3]=Base64Encode(password) + enter;
if(!Dialog(SendBuffer,"SMTP服务器验证失败,请核对用户名和密码。"))
return false;
}
else
{
SendBufferstr="HELO " + mailserver + enter;
if(!Dialog(SendBufferstr,""))
return false;
} //
SendBufferstr="MAIL FROM:<" + From + ">" + enter;
if(!Dialog(SendBufferstr,"发件人地址错误,或不能为空"))
return false; //
SendBuffer=new string[recipientmaxnum];
for(int i=0;i<Recipient.Count;i++)
{ SendBuffer[i]="RCPT TO:<" + Recipient[i].ToString() +">" + enter; }
if(!Dialog(SendBuffer,"收件人地址有误"))
return false; SendBufferstr="DATA" + enter;
if(!Dialog(SendBufferstr,""))
return false; SendBufferstr="From:" + FromName + "<" + From +">" +enter; SendBufferstr += "To:=¡"+Charset.ToUpper()+"¡B¡"+Base64Encode(RecipientName)+"¡="+"<"+Recipient[0]+">"+enter;
SendBufferstr+="CC:";
for(int i=0;i<Recipient.Count;i++)
{
SendBufferstr+=Recipient[i].ToString() + "<" + Recipient[i].ToString() +">,";
}
SendBufferstr+=enter;
if(Charset=="")
{
SendBufferstr+="Subject:" + Subject + enter;
}
else
{
SendBufferstr+="Subject:" + "=¡" + Charset.ToUpper() + "¡B¡" + Base64Encode(Subject) +"¡=" +enter;
} SendBufferstr+="X-Priority:" + priority + enter;
SendBufferstr+="X-MSMail-Priority:" + priority + enter;
SendBufferstr+="Importance:" + priority + enter;
SendBufferstr+="X-Mailer: Huolx.Pubclass" + enter;
SendBufferstr+="MIME-Version: 1.0" + enter; SendBufferstr += "Content-Type: multipart/mixed;"+enter;//内容格式和分隔符
SendBufferstr += " boundary=\"----=_NextPart_000_00D6_01C29593.AAB31770\""+enter;
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter; if(Html)
{
SendBufferstr+="Content-Type: text/html;" + enter;
}
else
{
SendBufferstr+="Content-Type: text/plain;" + enter;
} if(Charset=="")
{
SendBufferstr+=" charset=\"iso-8859-1\"" + enter;
}
else
{
SendBufferstr+=" charset=\"" + Charset.ToLower() + "\"" + enter;
}
//SendBufferstr += "Content-Transfer-Encoding: base64"+enter; SendBufferstr+="Content-Transfer-Encoding: base64" + enter + enter; SendBufferstr+= Base64Encode(Body) + enter;
if(Attachments.Count!=0)
{
foreach(string filepath in Attachments)
{
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770"+enter;
SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += " name=\"=¡"+Charset.ToUpper()+"¡B¡"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"¡=\""+enter;
SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
SendBufferstr += "Content-Disposition: attachment;"+enter;
SendBufferstr += " filename=\"=¡"+Charset.ToUpper()+"¡B¡"+Base64Encode(filepath.Substring(filepath.LastIndexOf("\\")+1))+"¡=\""+enter+enter;
SendBufferstr += GetStream(filepath)+enter+enter;
}
}
SendBufferstr += "------=_NextPart_000_00D6_01C29593.AAB31770--"+enter+enter;
SendBufferstr += enter + "." + enter; if(!Dialog(SendBufferstr,"错误信件信息"))
return false;
SendBufferstr="QUIT" + enter;
if(!Dialog(SendBufferstr,"断开连接时错误"))
return false;
ns.Close();
tc.Close();
return true;
}
/// <summary>
/// 发送邮件方法,所有参数均通过属性设置。
/// </summary>
public bool Send()
{
if(Recipient.Count==0)
{
errmsg="收件人列表不能为空";
return false;
} if(mailserver.Trim()=="")
{
errmsg="必须指定SMTP服务器";
return false;
} return SendEmail();
}
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="smtpserver">smtp服务器信息,如"username:passwordwww.smtpserver.com:25",也可去掉部分次要信息,如www.smtpserver.com"</param>
public bool Send(string smtpserver)
{
MailDomain=smtpserver;
return Send();
}
/// <summary>
/// 发送邮件方法
/// </summary>
/// <param name="smtpserver">smtp服务器信息,如"username:passwordwww.smtpserver.com:25",也可去掉部分次要信息,如www.smtpserver.com"</param>
/// <param name="from">发件人mail地址</param>
/// <param name="fromname">发件人姓名</param>
/// <param name="to">收件人地址</param>
/// <param name="toname">收件人姓名</param>
/// <param name="html">是否HTML邮件</param>
/// <param name="subject">邮件主题</param>
/// <param name="body">邮件正文</param>
public bool Send(string smtpserver,string from,string fromname,string to,string toname,bool html,string subject,string body)
{
MailDomain=smtpserver;
From=from;
FromName=fromname;
AddRecipient(to);
RecipientName=toname;
Html=html;
Subject=subject;
Body=body;
return Send();
}
}
}
“/SmtpMail”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引和长度必须引用该字符串内的位置。参数名: length
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引和长度必须引用该字符串内的位置。参数名: length源错误:
行 478: return false;
行 479: }
行 480: string RRCode=RR.Substring(0,3);
行 481: if(RightCodeHT[RRCode]!=null)
行 482: {
源文件: e:\hgoffice\smtpmail\esmtpmail.cs 行: 480 堆栈跟踪:
[ArgumentOutOfRangeException: 索引和长度必须引用该字符串内的位置。
参数名: length]
System.String.Substring(Int32 startIndex, Int32 length) +226
SmtpMail.ESmtpMail.Dialog(String str, String errstr) in e:\hgoffice\smtpmail\esmtpmail.cs:480
SmtpMail.ESmtpMail.SendEmail() in e:\hgoffice\smtpmail\esmtpmail.cs:670
SmtpMail.ESmtpMail.Send() in e:\hgoffice\smtpmail\esmtpmail.cs:697
SmtpMail.ESmtp.btnSend_Click(Object sender, EventArgs e) in e:\hgoffice\smtpmail\esmsamples.aspx.cs:121
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
检查RR有没有值,或者是长度有没有3个字符这么长
RecvResponse()是用来接收SMTP服务器回应 的
你把1,2,3里面的代码连起来看你就知道了
{
int StreamSize;
string Returnvalue = "";
byte[] ReadBuffer = new byte[1024] ;
try
{
StreamSize=ns.Read(ReadBuffer,0,ReadBuffer.Length);
}
catch
{
errmsg="网络连接错误";
return "false";
} if (StreamSize==0)
{
return Returnvalue ;
}
else
{
Returnvalue = Encoding.Default.GetString(ReadBuffer).Substring(0,StreamSize);
logs+=Returnvalue;
return Returnvalue;
}
}
出错的地方:
private bool Dialog(string str,string errstr)
{
if(str==null||str.Trim()=="")
{
return true;
}
if(SendCommand(str))
{
string RR=RecvResponse();
if(RR=="false")
{
return false;
}
string RRCode=RR.Substring(0,3);
if(RightCodeHT[RRCode]!=null)
{
return true;
}
else
{
if(ErrCodeHT[RRCode]!=null)
{
errmsg+=(RRCode+ErrCodeHT[RRCode].ToString());
errmsg+=enter;
}
else
{
errmsg+=RR;
}
errmsg+=errstr;
return false;
}
}
else
{
return false;
} }
调试一下,在这设个断点,看看这个RR是什么东东????????贴出来
RecvResponse()是用来接收SMTP服务器回应 的
你把1,2,3里面的代码连起来看你就知道了
你在string RR=RecvResponse(); 下面设置个断点,单步调一下。看看RR运行时得到的值。这个是常规错误,找原因最好调试。