求教高手,emos1.5,普通发信无误,用程序调用smtp失败。程序如下。为何?
private bool SendEmail()
{
//连接网络
try
{
tc = new TcpClient(mailserver, mailserverport);
}
catch (Exception e)
{
Trip8.Common.ILog.LogWrite(Trip8.Common.Configuration.LogEmail, "SendEmail()" + e.Message);
errmsg = e.ToString();
return false;
}
ns = tc.GetStream();
SMTPCodeAdd();
//验证网络连接是否正确
if (RightCodeHT[RecvResponse().Substring(0, 3)] == null)
{
Trip8.Common.ILog.LogWrite(Trip8.Common.Configuration.LogEmail, "SendEmail()+网络连接失败");
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服务器验证失败,请核对用户名和密码。"))
{
Trip8.Common.ILog.LogWrite(Trip8.Common.Configuration.LogEmail, "SendEmail()+SMTP服务器验证失败,请核对用户名和密码。");
return false;
}
}
else
{
SendBufferstr = "HELO " + mailserver + enter;
if (!Dialog(SendBufferstr, ""))
return false;
}
//
SendBufferstr = "MAIL FROM:<" + From + ">" + enter;
if (!Dialog(SendBufferstr, "发件人地址错误,或不能为空"))
{
Trip8.Common.ILog.LogWrite(Trip8.Common.Configuration.LogEmail, "SendEmail()+发件人地址错误,或不能为空。");
return false;
}
//
SendBuffer = new string[recipientmaxnum];
for (int i = 0; i < Recipient.Count; i++)
{
SendBuffer = "RCPT TO:<" + Recipient.ToString() + ">" + enter;
}
if (!Dialog(SendBuffer, "收件人地址有误"))
{
Trip8.Common.ILog.LogWrite(Trip8.Common.Configuration.LogEmail, "SendEmail()+收件人地址有误。");
return false;
}
/*
SendBuffer=new string[10];
for(int i=0;i<RecipientBCC.Count;i++)
{
SendBuffer="RCPT TO:<" + RecipientBCC.ToString() +">" + enter;
}
if(!Dialog(SendBuffer,"密件收件人地址有误"))
return false;
*/
SendBufferstr = "DATA" + enter;
if (!Dialog(SendBufferstr, ""))
return false;
SendBufferstr = "From:" + FromName + "<" + From + ">" + enter;
//if(ReplyTo.Trim()!="")
//{
// SendBufferstr+="Reply-To: " + ReplyTo + enter;
//}
//SendBufferstr+="To:" + RecipientName + "<" + Recipient[0] +">" +enter;
SendBufferstr += "To:=?" + Charset.ToUpper() + "?B?" + Base64Encode(RecipientName) + "?=" + "<" + Recipient[0] + ">" + enter;
SendBufferstr += "CC:";
for (int i = 1; i < Recipient.Count; i++)
{
SendBufferstr += Recipient.ToString() + "<" + Recipient.ToString() + ">,";
}
SendBufferstr += enter;
SendBufferstr += ((Subject == String.Empty || Subject == null) ? "Subject:" : ((Charset == "") ? ("Subject:" + Subject) : ("Subject:" + "=?" + Charset.ToUpper() + "?B?" + Base64Encode(Subject) + "?="))) + enter;
SendBufferstr += "X-Priority:" + priority.ToString() + enter;
SendBufferstr += "X-MSMail-Priority:" + priority.ToString() + enter;
SendBufferstr += "Importance:" + priority.ToString() + enter;
SendBufferstr += "X-Mailer: ProgramProbe.CommunicationProxy.SmtpMailHelper [cn]" + enter;
SendBufferstr += "MIME-Version: 1.0" + enter;
if (Attachments.Count != 0)
{
SendBufferstr += "Content-Type: multipart/mixed;" + enter;
SendBufferstr += " boundary=\"=====" + (Html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====\"" + enter + enter;
}
if (Html)
{
if (Attachments.Count == 0)
{
SendBufferstr += "Content-Type: multipart/alternative;" + enter;//内容格式和分隔符
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\"" + enter + enter;
SendBufferstr += "This is a multi-part message in MIME format." + enter + enter;
}
else
{
SendBufferstr += "This is a multi-part message in MIME format." + enter + enter;
SendBufferstr += "--=====001_Dragon520636771063_=====" + enter;
SendBufferstr += "Content-Type: multipart/alternative;" + enter;//内容格式和分隔符
SendBufferstr += " boundary=\"=====003_Dragon520636771063_=====\"" + enter + enter;
}
SendBufferstr += "--=====003_Dragon520636771063_=====" + enter;
SendBufferstr += "Content-Type: text/plain;" + enter;
SendBufferstr += ((Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + Charset.ToLower() + "\"")) + enter;
SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr += Base64Encode("邮件内容为HTML格式,请选择HTML方式查看") + enter + enter;
SendBufferstr += "--=====003_Dragon520636771063_=====" + enter;
SendBufferstr += "Content-Type: text/html;" + enter;
SendBufferstr += ((Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + Charset.ToLower() + "\"")) + enter;
SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr += Base64Encode(Body) + enter + enter;
SendBufferstr += "--=====003_Dragon520636771063_=====--" + enter;
}
else
{
if (Attachments.Count != 0)
{
SendBufferstr += "--=====001_Dragon303406132050_=====" + enter;
}
SendBufferstr += "Content-Type: text/plain;" + enter;
SendBufferstr += ((Charset == "") ? (" charset=\"iso-8859-1\"") : (" charset=\"" + Charset.ToLower() + "\"")) + enter;
SendBufferstr += "Content-Transfer-Encoding: base64" + enter + enter;
SendBufferstr += Base64Encode(Body) + enter;
}
//SendBufferstr += "Content-Transfer-Encoding: base64"+enter;
if (Attachments.Count != 0)
{
for (int i = 0; i < Attachments.Count; i++)
{
string filepath = (string)Attachments;
SendBufferstr += "--=====" + (Html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====" + enter;
//SendBufferstr += "Content-Type: application/octet-stream"+enter;
SendBufferstr += "Content-Type: text/plain;" + 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 += "--=====" + (Html ? "001_Dragon520636771063_" : "001_Dragon303406132050_") + "=====--" + enter + enter;
}
SendBufferstr += enter + "." + enter;
if (!Dialog(SendBufferstr, "错误信件信息"))
return false;
SendBufferstr = "QUIT" + enter;
if (!Dialog(SendBufferstr, "断开连接时错误"))
return false;
ns.Close();
tc.Close();
return true;
} |