472,353 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 5699
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: nick | last post by:
The code always returen the following error at fclose(fp). Fatal error: Maximum execution time of 30 seconds exceeded in...
3
by: marsandys | last post by:
Hello, I have this code below, which I am trying to have it send HTML formatted mail with embedded images. I can get this to send the mail, but it...
1
by: Sam | last post by:
Hi, I want to be able to send an email w/ attachment. I have an INPUT Type="File" HTMLControl on the page. User selects the attachment using this...
2
by: Robbie De Sutter | last post by:
Hello, How do I open a new, empty e-mail message from the default e-mail client whereby the sender is given and a file is attached? Currently...
2
by: Cimento Cola | last post by:
Hello all. Hope anyone can help me! I have this form, with: Please choose your file: (browse) The main purpose is the user to select a...
3
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. ...
1
by: shil | last post by:
Hi, I'm using FW 2.0 to send an email with an attachment using System.Net.Mail class. If I try to attach a file from my file system, I could...
1
by: Chitu03 | last post by:
Hi I am already send a mail using Php with some attachement into it. My Problem is the attachement file is in my Database(mysql). I don't know...
1
by: sxwend | last post by:
I am trying to use the following post results (http://www.thescripts.com/forum/thread189759.html) and add another requirement. I need to send the...
1
by: deepaks85 | last post by:
Dear All, I want to send some data through a form with Multiple attachment in an HTML Format. I have tried it but it is not working for me. I am...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.