469,648 Members | 1,197 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Send file attachment

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 = "su*****@mycompany.com";
string sTo = "we*******@bmycompany.com";
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.ToCharArr ay());
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
Nov 15 '05 #1
2 5582
Use MailMessage, MailAttachment and SmtpMail classes from MailAttachment
namespace

--
-------------------------
"Manish Agarwal"- <ma***********@hotmail.com>

"Jacques" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
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 = "su*****@mycompany.com";
string sTo = "we*******@bmycompany.com";
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.ToCharArr ay());
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

Nov 15 '05 #2
Use MailMessage, MailAttachment and SmtpMail classes from System.Web.Mail
namespace
--
-------------------------
"Manish Agarwal"- <ma***********@hotmail.com>

"Jacques" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
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 = "su*****@mycompany.com";
string sTo = "we*******@bmycompany.com";
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.ToCharArr ay());
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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by nick | last post: by
3 posts views Thread by marsandys | last post: by
1 post views Thread by Sam | last post: by
2 posts views Thread by Cimento Cola | last post: by
3 posts views Thread by =?Utf-8?B?SHVnaA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.