/// <param name="mailmessage"></param>
/// <returns></returns>
public bool sendemail(string smtpserver,int port,mailmessage mailmessage)
{
return sendemail(smtpserver,port,false,"","",mailmessage);
}
/// <summary>
/// 发送电子邮件,smtp服务器需要身份验证
/// </summary>
/// <param name="smtpserver"></param>
/// <param name="port"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="mailmessage"></param>
/// <returns></returns>
public bool sendemail(string smtpserver,int port,string username,string password,mailmessage mailmessage)
{
return sendemail(smtpserver,port,false,username,password,mailmessage);
}
private bool sendemail(string smtpserver,int port,bool esmtp,string username,string password,mailmessage mailmessage)
{
if (connect(smtpserver,port)==false)//测试连接服务器是否成功
return false;
string priority=getprioritystring(mailmessage.priority);
bool html=(mailmessage.bodyformat==mailformat.html);
string[] sendbuffer;
string sendbufferstr;
//进行smtp验证,现在大部分smtp服务器都要认证
if(esmtp)
{
sendbuffer=new string[4];
sendbuffer[0]="ehlo " + smtpserver + crlf;
sendbuffer[1]="auth login" + crlf;
sendbuffer[2]=base64encode(username) + crlf;
sendbuffer[3]=base64encode(password) + crlf;
if(!dialog(sendbuffer,"smtp服务器验证失败,请核对用户名和密码。"))
return false;
}
else
{//不需要身份认证
sendbufferstr="helo " + smtpserver + crlf;
if(!dialog(sendbufferstr,""))
return false;
}
//发件人地址
sendbufferstr="mail from:<" + mailmessage.from + ">" + crlf;
if(!dialog(sendbufferstr,"发件人地址错误,或不能为空"))
return false;
//收件人地址
sendbuffer=new string[mailmessage.recipients.count];
for(int i=0;i<mailmessage.recipients.count;i++)
{
sendbuffer[i]="rcpt to:<" +(string)mailmessage.recipients[i] +">" + crlf;
}
if(!dialog(sendbuffer,"收件人地址有误"))
return false;
/*
sendbuffer=new string[10];
for(int i=0;i<recipientbcc.count;i++)
{
sendbuffer[i]="rcpt to:<" + recipientbcc[i].tostring() +">" + crlf;
}
if(!dialog(sendbuffer,"密件收件人地址有误"))
return false;
*/
sendbufferstr="data" + crlf;
if(!dialog(sendbufferstr,""))
return false;
//发件人姓名
sendbufferstr="from:" + mailmessage.fromname + "<" +mailmessage.from +">" +crlf;
//if(replyto.trim()!="")
//{
// sendbufferstr+="reply-to: " + replyto + crlf;
//}
//sendbufferstr+="to:" + toname + "<" + recipient[0] +">" +crlf;
//至少要有一个收件人
if (mailmessage.recipients.count==0)
{
return false;
}
else
{
sendbufferstr += "to:=?"+mailmessage.charset.toupper()+"?b?"+
base64encode((string)mailmessage.recipients[0])+"?="+"<"+(string)mailmessage.recipients[0]+">"+crlf;
}
//sendbufferstr+="cc:";
//for(int i=0;i<recipient.count;i++)
//{
// sendbufferstr+=recipient[i].tostring() + "<" + recipient[i].tostring() +">,";
//}
//sendbufferstr+=crlf;
sendbufferstr+=