本文发表在 rolia.net 枫下论坛using System;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.IO;
namespace MyCommon
{
public class Attachment
{
public string filePath;
public string fileName;
public Attachment(string AttachmentPath,string AttachmentName)
{
filePath = AttachmentPath;
fileName = AttachmentName;
}
public Attachment(string AttachmentPath)
{
filePath = AttachmentPath;
fileName = Path.GetFileName(AttachmentPath);
}
public override bool Equals(object obj)
{
if(obj is Attachment)
{
Attachment att = (Attachment)obj;
return (att.fileName == this.fileName && att.filePath == this.filePath);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return base.GetHashCode ();
}
}
public class SmtpClient
{
private string _SmtpServer;
private int _Prot;
private string _UserName;
private string _Password;
public string Sender = string.Empty;
public string SenderName = string.Empty;
public ArrayList Receivers = new ArrayList();
public ArrayList ccReceivers = new ArrayList();
public ArrayList bccReceivers = new ArrayList();
private ArrayList Attachments = new ArrayList();
public string Subject = string.Empty;
public string Body = string.Empty;
public bool IsHtmlBody = false;
public string AlternativeBody = string.Empty;
private bool _login = false;
private string _mimeVersion = "MIME-Version: 1.0\r\n";
private string _mimeHTMLType = "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
private string _mimeTextType = "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
private string _mimeApplicationType1 = "Content-Type: application/octet-stream; \r\n";
private string _mimeApplicationType2 = "Content-disposition: attachment; filename=\"@filename\"\r\n";
private string _mimeMultiType = "Content-Type: multipart/mixed; boundary=\"abc\"\r\n";
private string _mimeMultiTypeAlt = "Content-Type: multipart/alternative; boundary=\"abc\"\r\n";
private string _encodingBase64String = "Content-transfer-encoding: base64\r\n\r\n";
private string _encodingString = "Content-transfer-encoding: 8bit\r\n\r\n";
private string _boundary = "\r\n\r\n--abc\r\n";
private string _boundayend = "\r\n\r\n--abc--\r\n";
private bool _HasAttatchment = false;
private bool _NeedEndBoundary = false;
private string _HTMLHeader = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"">" + "\r\n";
private TcpClient _mailClient = null;
public SmtpClient(){}
public SmtpClient(string SmtpServer,int Port,string UserName,string Password)
{
init(SmtpServer,Port,UserName,Password,true);
}
public SmtpClient(string SmtpServer,string UserName,string Password)
{
init(SmtpServer,25,UserName,Password,true);
}
public SmtpClient(string SmtpServer,int Port)
{
init(SmtpServer,Port,string.Empty,string.Empty,false);
}
public SmtpClient(string SmtpServer)
{
init(SmtpServer,25,string.Empty,string.Empty,false);
}
private void init(string SmtpServer,int Port,string UserName,string Password,bool Login)
{
_SmtpServer = SmtpServer;
_Prot = Port;
_UserName = Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName));
_Password = Convert.ToBase64String(Encoding.ASCII.GetBytes(Password));
_login = Login;
}
public void addAttachment(Attachment attachment)
{
Attachments.Add(attachment);
}
public void clearAttachments()
{
Attachments.Clear();
}
public void removeAttachment(Attachment attachment)
{
Attachments.Remove(attachment);
}
private string[] getMXServer(string DomainName)
{
ArrayList mailServer = new ArrayList();
DNS dns = new DNS();
IEnumerator ienum = dns.LookupMX(DomainName);
while( ienum.MoveNext() )
{
DNS_WRAPPER dnsentry = (DNS_WRAPPER)ienum.Current;
if( dnsentry.dnsType == DNSTypes.DNS_TYPE_MX )
{
DNS_MX_DATA dnsmx = (DNS_MX_DATA)dnsentry.dnsData;
mailServer.Add(dnsmx.pNameExchange);
}
}
if(mailServer.Count >0)
return (string[])mailServer.ToArray(typeof(string));
else
throw new Exception("Can not find Mail Server for domin " + DomainName);
}
private string getServerResponse()
{
byte[] serverbuff = new Byte[2048];
NetworkStream stream = _mailClient.GetStream();
int count = stream.Read( serverbuff, 0, 2048 );
if (count == 0)
{
return "";
}
return Encoding.ASCII.GetString( serverbuff, 0, count );
}
private string getEncodingString(bool useBase64)
{
if(useBase64)
{
return _encodingBase64String;
}
else
{
return _encodingString;
}
}
private void WriteCommand(string message)
{
byte[] WriteBuffer = Encoding.ASCII.GetBytes(message) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void WriteMessage(string message,Encoding encoder)
{
byte[] WriteBuffer = encoder.GetBytes(message) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void WriteMessage(string message,Encoding encoder, bool useBase64)
{
if(useBase64)
{
writeMessageBase64(message);
}
else
{
WriteMessage(message,encoder);
}
}
private void writeMessageBase64(string message)
{
byte[] WriteBuffer = Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.ASCII.GetBytes(message))) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void writeFileBase64(string FileName)
{
int length = 57;
FileStream fs;
byte[] buffer = new byte[length];
int readLength;
byte[] WriteBuffer;
if(File.Exists(FileName))
{
//FileStream _fs = new FileStream(@"c:\readDate",FileMode.Create);
NetworkStream stream = _mailClient.GetStream() ;
FileInfo fInfo = new FileInfo(FileName);
long filelength = fInfo.Length;
fs = File.OpenRead(FileName);
for(long i =0;i<filelength;i+= readLength)
{
fs.Position = i;
readLength = fs.Read(buffer,0,length);
WriteBuffer = Encoding.ASCII.GetBytes(Convert.ToBase64String(buffer,0,readLength)+ "\r\n") ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
//_fs.Write(WriteBuffer,0,WriteBuffer.Length);
}
fs.Close();
//_fs.Close();
}
}
private void writeFile(string FileName)
{
int length = 2048;
FileStream fs;
byte[] buffer = new byte[length];
int readLength;
byte[] WriteBuffer;
if(File.Exists(FileName))
{
//FileStream _fs = new FileStream(@"c:\readDate",FileMode.Create);
NetworkStream stream = _mailClient.GetStream() ;
FileInfo fInfo = new FileInfo(FileName);
long filelength = fInfo.Length;
fs = File.OpenRead(FileName);
for(long i =0;i<filelength;i+= readLength)
{
fs.Position = i;
//_fs.Position = i;
readLength = fs.Read(buffer,0,length);
WriteBuffer = Encoding.ASCII.GetBytes(Encoding.UTF8.GetString(buffer,0,readLength)) ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
//_fs.Write(WriteBuffer,0,WriteBuffer.Length);
}
fs.Close();
//_fs.Close();
}
}
private void writeFile(string FileName,bool useBase64)
{
if(useBase64)
{
writeFileBase64(FileName);
}
else
{
writeFile(FileName);
}
}
private void sendEmailOneByOne(string EmailAddress)
{
string message;
string response;
_NeedEndBoundary = false;
bool connected = false;
int serverIndex = 0;
string log = string.Empty;
try
{
_mailClient = new TcpClient();
string[] serverName = new string[1];
if(_SmtpServer != string.Empty)
serverName[0] = _SmtpServer;
else
{
string[] temp = EmailAddress.ToString().Split(new char[]{'@'});
serverName = getMXServer(temp[1]);
}
while(!connected && serverIndex < serverName.Length)
{
try
{
_mailClient.Connect(serverName[0],25);
connected = true;
}
catch
{
}
serverIndex++;
}
if(!connected)
throw new Exception("Cannot connect to Mail Server ");
response = getServerResponse();
if (response.Substring(0, 3) != "220")
{
throw new Exception(response);
}
message = "HELO \r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
message = "HELO eclose \r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
}
if(_login)
{
message = "AUTH LOGIN\r\n";
WriteCommand(message);
response = getServerResponse();
if( response != "334 VXNlcm5hbWU6\r\n")
{
throw new Exception(response);
}
WriteCommand(_UserName + "\r\n");
response = getServerResponse();
if( response != "334 UGFzc3dvcmQ6\r\n")
{
throw new Exception(response);
}
WriteCommand(_Password + "\r\n");
response = getServerResponse();
if (response.Substring(0, 3) != "235")
{
throw new Exception(response);
}
}
message = "MAIL FROM: <" + Sender + ">\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "RCPT TO:<" + EmailAddress + ">\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "DATA\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "354")
{
throw new Exception(response);
}
message = "From: " + "\"" + (SenderName==string.Empty?Sender:SenderName) + "\"<" + (Sender==string.Empty?SenderName:Sender) + ">" + "\r\n";
foreach ( string address in Receivers )
{
message += "To: " + "\"" + address + "\"<" + address + ">" + "\r\n";
}
// foreach ( string address in ccReceivers )
// {
// message += "Cc: " + "\"" + address + "\"<" + address + ">" + "\r\n";
// }
message += "Subject: " + Subject + "\r\n";
message += "Date: " + DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00") + "\r\n";
WriteCommand(message);
_HasAttatchment = (Attachments.Count > 0);
message = _mimeVersion;
message += "X-Mailer: Internet Mail Service (5.5.2650.21)\r\n";
if(_HasAttatchment)
{
message += _mimeMultiType;
_NeedEndBoundary = true;
}
else if(IsHtmlBody)
{
message += _mimeMultiTypeAlt;
_NeedEndBoundary = true;
}
message += "Return-Path: " + (Sender==string.Empty?SenderName:Sender) + "\r\n";
if(_NeedEndBoundary)
message += _boundary;
message += _mimeTextType;
message += getEncodingString(false);
WriteCommand(message);
if(IsHtmlBody)
{
if(Body.IndexOf(_HTMLHeader) < 0)
{
Body = _HTMLHeader + Body;
}
if(AlternativeBody != null && AlternativeBody != string.Empty)
{
message = AlternativeBody;
}
else
{
message = "Your email program settings cannot read HTML format email.\r\n";
}
message += _boundary;
message += _mimeHTMLType;
message += getEncodingString(false);
WriteCommand(message);
}
WriteMessage(Body,Encoding.ASCII,false);
if(_HasAttatchment)
{
foreach(Attachment attachment in Attachments)
{
message = _boundary + _mimeApplicationType1 + _mimeApplicationType2.Replace("@filename",attachment.fileName) + getEncodingString(true) ;
WriteCommand(message);
writeFile(attachment.filePath,true);
}
}
if(_NeedEndBoundary)
{
WriteCommand(_boundayend);
}
// StreamReader sr = File.OpenText(@"c:\mailInfo.txt");
// message = sr.ReadToEnd();
// sr.Close();
// WriteCommand(message);
message = "\r\n.\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "QUIT\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.IndexOf("221") == -1)
{
throw new Exception(response);
}
}
catch(Exception e)
{
throw e;
}
finally
{
_mailClient.Close();
}
}
public void Send()
{
foreach ( string address in Receivers )
{
sendEmailOneByOne(address);
}
foreach ( string address in ccReceivers )
{
sendEmailOneByOne(address);
}
foreach ( string address in bccReceivers )
{
sendEmailOneByOne(address);
}
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.IO;
namespace MyCommon
{
public class Attachment
{
public string filePath;
public string fileName;
public Attachment(string AttachmentPath,string AttachmentName)
{
filePath = AttachmentPath;
fileName = AttachmentName;
}
public Attachment(string AttachmentPath)
{
filePath = AttachmentPath;
fileName = Path.GetFileName(AttachmentPath);
}
public override bool Equals(object obj)
{
if(obj is Attachment)
{
Attachment att = (Attachment)obj;
return (att.fileName == this.fileName && att.filePath == this.filePath);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return base.GetHashCode ();
}
}
public class SmtpClient
{
private string _SmtpServer;
private int _Prot;
private string _UserName;
private string _Password;
public string Sender = string.Empty;
public string SenderName = string.Empty;
public ArrayList Receivers = new ArrayList();
public ArrayList ccReceivers = new ArrayList();
public ArrayList bccReceivers = new ArrayList();
private ArrayList Attachments = new ArrayList();
public string Subject = string.Empty;
public string Body = string.Empty;
public bool IsHtmlBody = false;
public string AlternativeBody = string.Empty;
private bool _login = false;
private string _mimeVersion = "MIME-Version: 1.0\r\n";
private string _mimeHTMLType = "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
private string _mimeTextType = "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
private string _mimeApplicationType1 = "Content-Type: application/octet-stream; \r\n";
private string _mimeApplicationType2 = "Content-disposition: attachment; filename=\"@filename\"\r\n";
private string _mimeMultiType = "Content-Type: multipart/mixed; boundary=\"abc\"\r\n";
private string _mimeMultiTypeAlt = "Content-Type: multipart/alternative; boundary=\"abc\"\r\n";
private string _encodingBase64String = "Content-transfer-encoding: base64\r\n\r\n";
private string _encodingString = "Content-transfer-encoding: 8bit\r\n\r\n";
private string _boundary = "\r\n\r\n--abc\r\n";
private string _boundayend = "\r\n\r\n--abc--\r\n";
private bool _HasAttatchment = false;
private bool _NeedEndBoundary = false;
private string _HTMLHeader = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd"">" + "\r\n";
private TcpClient _mailClient = null;
public SmtpClient(){}
public SmtpClient(string SmtpServer,int Port,string UserName,string Password)
{
init(SmtpServer,Port,UserName,Password,true);
}
public SmtpClient(string SmtpServer,string UserName,string Password)
{
init(SmtpServer,25,UserName,Password,true);
}
public SmtpClient(string SmtpServer,int Port)
{
init(SmtpServer,Port,string.Empty,string.Empty,false);
}
public SmtpClient(string SmtpServer)
{
init(SmtpServer,25,string.Empty,string.Empty,false);
}
private void init(string SmtpServer,int Port,string UserName,string Password,bool Login)
{
_SmtpServer = SmtpServer;
_Prot = Port;
_UserName = Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName));
_Password = Convert.ToBase64String(Encoding.ASCII.GetBytes(Password));
_login = Login;
}
public void addAttachment(Attachment attachment)
{
Attachments.Add(attachment);
}
public void clearAttachments()
{
Attachments.Clear();
}
public void removeAttachment(Attachment attachment)
{
Attachments.Remove(attachment);
}
private string[] getMXServer(string DomainName)
{
ArrayList mailServer = new ArrayList();
DNS dns = new DNS();
IEnumerator ienum = dns.LookupMX(DomainName);
while( ienum.MoveNext() )
{
DNS_WRAPPER dnsentry = (DNS_WRAPPER)ienum.Current;
if( dnsentry.dnsType == DNSTypes.DNS_TYPE_MX )
{
DNS_MX_DATA dnsmx = (DNS_MX_DATA)dnsentry.dnsData;
mailServer.Add(dnsmx.pNameExchange);
}
}
if(mailServer.Count >0)
return (string[])mailServer.ToArray(typeof(string));
else
throw new Exception("Can not find Mail Server for domin " + DomainName);
}
private string getServerResponse()
{
byte[] serverbuff = new Byte[2048];
NetworkStream stream = _mailClient.GetStream();
int count = stream.Read( serverbuff, 0, 2048 );
if (count == 0)
{
return "";
}
return Encoding.ASCII.GetString( serverbuff, 0, count );
}
private string getEncodingString(bool useBase64)
{
if(useBase64)
{
return _encodingBase64String;
}
else
{
return _encodingString;
}
}
private void WriteCommand(string message)
{
byte[] WriteBuffer = Encoding.ASCII.GetBytes(message) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void WriteMessage(string message,Encoding encoder)
{
byte[] WriteBuffer = encoder.GetBytes(message) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void WriteMessage(string message,Encoding encoder, bool useBase64)
{
if(useBase64)
{
writeMessageBase64(message);
}
else
{
WriteMessage(message,encoder);
}
}
private void writeMessageBase64(string message)
{
byte[] WriteBuffer = Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.ASCII.GetBytes(message))) ;
NetworkStream stream = _mailClient.GetStream() ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
}
private void writeFileBase64(string FileName)
{
int length = 57;
FileStream fs;
byte[] buffer = new byte[length];
int readLength;
byte[] WriteBuffer;
if(File.Exists(FileName))
{
//FileStream _fs = new FileStream(@"c:\readDate",FileMode.Create);
NetworkStream stream = _mailClient.GetStream() ;
FileInfo fInfo = new FileInfo(FileName);
long filelength = fInfo.Length;
fs = File.OpenRead(FileName);
for(long i =0;i<filelength;i+= readLength)
{
fs.Position = i;
readLength = fs.Read(buffer,0,length);
WriteBuffer = Encoding.ASCII.GetBytes(Convert.ToBase64String(buffer,0,readLength)+ "\r\n") ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
//_fs.Write(WriteBuffer,0,WriteBuffer.Length);
}
fs.Close();
//_fs.Close();
}
}
private void writeFile(string FileName)
{
int length = 2048;
FileStream fs;
byte[] buffer = new byte[length];
int readLength;
byte[] WriteBuffer;
if(File.Exists(FileName))
{
//FileStream _fs = new FileStream(@"c:\readDate",FileMode.Create);
NetworkStream stream = _mailClient.GetStream() ;
FileInfo fInfo = new FileInfo(FileName);
long filelength = fInfo.Length;
fs = File.OpenRead(FileName);
for(long i =0;i<filelength;i+= readLength)
{
fs.Position = i;
//_fs.Position = i;
readLength = fs.Read(buffer,0,length);
WriteBuffer = Encoding.ASCII.GetBytes(Encoding.UTF8.GetString(buffer,0,readLength)) ;
stream.Write(WriteBuffer,0,WriteBuffer.Length);
//_fs.Write(WriteBuffer,0,WriteBuffer.Length);
}
fs.Close();
//_fs.Close();
}
}
private void writeFile(string FileName,bool useBase64)
{
if(useBase64)
{
writeFileBase64(FileName);
}
else
{
writeFile(FileName);
}
}
private void sendEmailOneByOne(string EmailAddress)
{
string message;
string response;
_NeedEndBoundary = false;
bool connected = false;
int serverIndex = 0;
string log = string.Empty;
try
{
_mailClient = new TcpClient();
string[] serverName = new string[1];
if(_SmtpServer != string.Empty)
serverName[0] = _SmtpServer;
else
{
string[] temp = EmailAddress.ToString().Split(new char[]{'@'});
serverName = getMXServer(temp[1]);
}
while(!connected && serverIndex < serverName.Length)
{
try
{
_mailClient.Connect(serverName[0],25);
connected = true;
}
catch
{
}
serverIndex++;
}
if(!connected)
throw new Exception("Cannot connect to Mail Server ");
response = getServerResponse();
if (response.Substring(0, 3) != "220")
{
throw new Exception(response);
}
message = "HELO \r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
message = "HELO eclose \r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
}
if(_login)
{
message = "AUTH LOGIN\r\n";
WriteCommand(message);
response = getServerResponse();
if( response != "334 VXNlcm5hbWU6\r\n")
{
throw new Exception(response);
}
WriteCommand(_UserName + "\r\n");
response = getServerResponse();
if( response != "334 UGFzc3dvcmQ6\r\n")
{
throw new Exception(response);
}
WriteCommand(_Password + "\r\n");
response = getServerResponse();
if (response.Substring(0, 3) != "235")
{
throw new Exception(response);
}
}
message = "MAIL FROM: <" + Sender + ">\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "RCPT TO:<" + EmailAddress + ">\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "DATA\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "354")
{
throw new Exception(response);
}
message = "From: " + "\"" + (SenderName==string.Empty?Sender:SenderName) + "\"<" + (Sender==string.Empty?SenderName:Sender) + ">" + "\r\n";
foreach ( string address in Receivers )
{
message += "To: " + "\"" + address + "\"<" + address + ">" + "\r\n";
}
// foreach ( string address in ccReceivers )
// {
// message += "Cc: " + "\"" + address + "\"<" + address + ">" + "\r\n";
// }
message += "Subject: " + Subject + "\r\n";
message += "Date: " + DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zz00") + "\r\n";
WriteCommand(message);
_HasAttatchment = (Attachments.Count > 0);
message = _mimeVersion;
message += "X-Mailer: Internet Mail Service (5.5.2650.21)\r\n";
if(_HasAttatchment)
{
message += _mimeMultiType;
_NeedEndBoundary = true;
}
else if(IsHtmlBody)
{
message += _mimeMultiTypeAlt;
_NeedEndBoundary = true;
}
message += "Return-Path: " + (Sender==string.Empty?SenderName:Sender) + "\r\n";
if(_NeedEndBoundary)
message += _boundary;
message += _mimeTextType;
message += getEncodingString(false);
WriteCommand(message);
if(IsHtmlBody)
{
if(Body.IndexOf(_HTMLHeader) < 0)
{
Body = _HTMLHeader + Body;
}
if(AlternativeBody != null && AlternativeBody != string.Empty)
{
message = AlternativeBody;
}
else
{
message = "Your email program settings cannot read HTML format email.\r\n";
}
message += _boundary;
message += _mimeHTMLType;
message += getEncodingString(false);
WriteCommand(message);
}
WriteMessage(Body,Encoding.ASCII,false);
if(_HasAttatchment)
{
foreach(Attachment attachment in Attachments)
{
message = _boundary + _mimeApplicationType1 + _mimeApplicationType2.Replace("@filename",attachment.fileName) + getEncodingString(true) ;
WriteCommand(message);
writeFile(attachment.filePath,true);
}
}
if(_NeedEndBoundary)
{
WriteCommand(_boundayend);
}
// StreamReader sr = File.OpenText(@"c:\mailInfo.txt");
// message = sr.ReadToEnd();
// sr.Close();
// WriteCommand(message);
message = "\r\n.\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.Substring(0, 3) != "250")
{
throw new Exception(response);
}
message = "QUIT\r\n";
WriteCommand(message);
response = getServerResponse();
if (response.IndexOf("221") == -1)
{
throw new Exception(response);
}
}
catch(Exception e)
{
throw e;
}
finally
{
_mailClient.Close();
}
}
public void Send()
{
foreach ( string address in Receivers )
{
sendEmailOneByOne(address);
}
foreach ( string address in ccReceivers )
{
sendEmailOneByOne(address);
}
foreach ( string address in bccReceivers )
{
sendEmailOneByOne(address);
}
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net