can't get multiple files when attachment through stream class.
-
Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU
-
Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU
pujafaldu wrote:
but if i want to attach multiple files then it overwrite to first file with second.
How are you attaching multple files? The code only seems to work as far as there is one attachment. In fact your method should look like:
public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, List<string> PlstFileNames, List<Stream> PlstStreams)
{
MailAddress from = new MailAddress(Config.MailFrom);
MailAddress to = new MailAddress(strToEmail);MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; for(int LiCount = 0; LiCount < PlstFileNames.Count; LiCount++) { Attachment attachFile = new Attachment(PlstStreams\[LiCount\], PlstFileNames\[LiCount\]); mail.Attachments.Add(attachFile); } SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); }
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
pujafaldu wrote:
but if i want to attach multiple files then it overwrite to first file with second.
How are you attaching multple files? The code only seems to work as far as there is one attachment. In fact your method should look like:
public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, List<string> PlstFileNames, List<Stream> PlstStreams)
{
MailAddress from = new MailAddress(Config.MailFrom);
MailAddress to = new MailAddress(strToEmail);MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; for(int LiCount = 0; LiCount < PlstFileNames.Count; LiCount++) { Attachment attachFile = new Attachment(PlstStreams\[LiCount\], PlstFileNames\[LiCount\]); mail.Attachments.Add(attachFile); } SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); }
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
my question is here! List PlstStreams , how can i create group of stream for multiple files. i have code for stream in another file which pass above values. string strAttach = string.Empty; string strSendAttach = string.Empty; string strFileName = string.Empty; string strFileAttachment = string.Empty; Stream FileStream; bool bFlag = false; bool bCheck = false; HttpFileCollection hfc = Request.Files; if (hfc.Count != 0) { for (int i = 0; i < hfc.Count; i++) { if (!string.IsNullOrEmpty(hfc[i].FileName)) { FileStream = hfc[i].InputStream; strFileAttachment = hfc[i].FileName; if (objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, strFileAttachment, FileStream, bCheck)) { lblMessage.Visible = true; lblMessage.Text = "Mail has been sent successfully."; lblMessage.CssClass = "succLabel"; } } } } Thanks. Regards, PUJA FALDU
-
Hi... here i have send mail with attachment file. i have used stream class for sending attachment. Here is code...... public bool SendMailToExternalUser(string strToEmail, string strbody, string strSubject, string strFileName, Stream FileStream,bool bCheck) { bResult = false; MailAddress from = new MailAddress(Config.MailFrom); MailAddress to = new MailAddress(strToEmail); MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; Attachment attachFile = new Attachment(FileStream, strFileName); mail.Attachments.Add(attachFile); if (bCheck == true) { SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); bResult = true; } bResult = true; } this works fine when there is only one file to attach. but if i want to attach multiple files then it overwrite to first file with second. how can i get more then one file in FileStream? kindly help to solvethis issue. Thanks. Regards, PUJA FALDU
I realize this doesn't answer your code-specific question, but is zipping the files an option? It will provide better speed and reliably, and limit your attachments to a single file. You can use the
System.IO.Compression
class to compress the stream of files. Advantages of zipping files for email: Zip or unzip a file[^]Regards, Gary
-
my question is here! List PlstStreams , how can i create group of stream for multiple files. i have code for stream in another file which pass above values. string strAttach = string.Empty; string strSendAttach = string.Empty; string strFileName = string.Empty; string strFileAttachment = string.Empty; Stream FileStream; bool bFlag = false; bool bCheck = false; HttpFileCollection hfc = Request.Files; if (hfc.Count != 0) { for (int i = 0; i < hfc.Count; i++) { if (!string.IsNullOrEmpty(hfc[i].FileName)) { FileStream = hfc[i].InputStream; strFileAttachment = hfc[i].FileName; if (objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, strFileAttachment, FileStream, bCheck)) { lblMessage.Visible = true; lblMessage.Text = "Mail has been sent successfully."; lblMessage.CssClass = "succLabel"; } } } } Thanks. Regards, PUJA FALDU
May be you can do this:
string strAttach = string.Empty;
string strSendAttach = string.Empty;
string strFileName = string.Empty;
string strFileAttachment = string.Empty;
Stream FileStream;
bool bFlag = false;
bool bCheck = false;HttpFileCollection hfc = Request.Files;
objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, hfc);public void SendMailToExternalUser(string strToEmail, string strbody, string strSubject, HttpFileCollection PobjFileCollection)
{
MailAddress from = new MailAddress(Config.MailFrom);
MailAddress to = new MailAddress(strToEmail);MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; for(int LiCount = 0; LiCount < PobjFileCollection.Count; LiCount++) { Attachment attachFile = new Attachment(PobjFileCollection\[LiCount\].InputStream, PobjFileCollection\[LiCount\].FileName); mail.Attachments.Add(attachFile); } SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); }
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
-
May be you can do this:
string strAttach = string.Empty;
string strSendAttach = string.Empty;
string strFileName = string.Empty;
string strFileAttachment = string.Empty;
Stream FileStream;
bool bFlag = false;
bool bCheck = false;HttpFileCollection hfc = Request.Files;
objMailSend.SendMailToExternalUser(ValidetEmailId, txtMailBody.Text, lblSubject.Text, hfc);public void SendMailToExternalUser(string strToEmail, string strbody, string strSubject, HttpFileCollection PobjFileCollection)
{
MailAddress from = new MailAddress(Config.MailFrom);
MailAddress to = new MailAddress(strToEmail);MailMessage mail = new MailMessage(from, to); mail.Subject = strSubject; mail.Body = strbody; mail.IsBodyHtml = true; mail.Sender = from; for(int LiCount = 0; LiCount < PobjFileCollection.Count; LiCount++) { Attachment attachFile = new Attachment(PobjFileCollection\[LiCount\].InputStream, PobjFileCollection\[LiCount\].FileName); mail.Attachments.Add(attachFile); } SmtpClient SmtpMail = new SmtpClient(Config.MailServerSmtp); SmtpMail.Send(mail); }
Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.