Send file attachment

  • Thread starter Thread starter Jacques
  • Start date Start date
J

Jacques

Hello All,

This sends an email message OK. But, how do I
attach a file?

// create server SMTP with port 25
string ServSMTP = "mycompany.com";
string sFrom = "(e-mail address removed)";
string sTo = "(e-mail address removed)";
string sSubject = "Subject Test";
string sMessage = "Message Body Test";

TcpClient SmtpServ = new TcpClient(ServSMTP,25);

string Data;
byte[] szData;
string CRLF = "\r\n";


try
{
// initialization
NetworkStream NetStrm = SmtpServ.GetStream();
StreamReader RdStrm= new StreamReader(SmtpServ.GetStream
());

// say hello to server and send response into log report
Data = "EHLO mycompany.com " + CRLF; szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);


// send sender data
Data = "MAIL FROM:" + sFrom + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes
(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

// send receiver data
Data = "RCPT TO:" + sTo + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes
(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

// send DATA
Data = "DATA " + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes
(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

// send content data
Data = "SUBJECT: " + sSubject + CRLF + sMessage + CRLF
+ "." + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes
(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

// quit from server SMTP
Data = "QUIT " + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes
(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

// close connection
NetStrm.Close();
RdStrm.Close();

TIA,
Jacques
 
Use MailMessage, MailAttachment and SmtpMail classes from MailAttachment
namespace
 
Use MailMessage, MailAttachment and SmtpMail classes from System.Web.Mail
namespace
 
Back
Top