Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i want to send email with attachment. so when i attach a file and sending email the file is received but no content was displaying the file shows 0KB when i download from mail.

here is the code in aspx page

C#
string frommail =" from@mail.com";
               string subject="Demo mail";
               string body="check mail have a attachment";
               if (FileUpload1.HasFile.Equals(true))
               {
                   byte[] fileSize = new byte[FileUpload1.PostedFile.ContentLength];
                   HttpPostedFile uploadedfile = FileUpload1.PostedFile;
                   uploadedfile.InputStream.Read(fileSize, 0, (int)FileUpload1.PostedFile.ContentLength);
                    string str = txtto.Text.Replace(',',';');
                    emailaddress = str.Split(';');
                   foreach (string tomail in emailaddress)
                   {
                       if (tomail != "")
                       {
                           Boolean mail = _objsendmail.Sendmailwithattachment(frommail,tomail,subject,body,uploadedfile,fileSize);
                           if (mail == true)
                           {
                               Label1.Text = "mail sent successfully";
                               txtto.Text = "";
                               txtsubject.Text = "";
                               MailEditor.Value = "";
                           }
                       }
                   }
               }



code in
Sendmailwithattachment function


public Boolean Sendmailwithattachment(string from, string to, string subject, string body, HttpPostedFile uploadedfile, byte[] fileSize)
       {

           MailMessage message = new MailMessage();
           message.From = new MailAddress(from);
           string str = to.Replace(',', ';');
           emailaddress = str.Split(';');

           foreach (string toaddress in emailaddress)
           {
               if (toaddress != "")
               {
                   message.To.Add(toaddress);
                   message.Subject = subject;
                   message.Body = body;
                   message.Attachments.Add(new Attachment(uploadedfile.InputStream,uploadedfile.FileName));
                   message.IsBodyHtml = true;
                   SmtpClient emails = new SmtpClient("10.0.0.1");
                   emails.Send(message);
                   return true;
               }
           }
           return false;
       }


what is wrong with the code how can i pass my attach file in these function.

please help.

thanks,
parithi
Posted
Comments
Guirec 12-Feb-13 0:37am    
You should try to save your file to disk before giving it to the SmtpClient class.
1. you can make sure your file is actually not empty before you send
2. when sending the fule it is most likely that the HttpResponse (container for your input stream) has been disposed already.
Kishor Deshpande 18-Feb-13 5:50am    
This is repeated question, please have a look if it already exists before posting..

I would change the this line:
message.Attachments.Add(new Attachment(uploadedfile.InputStream,uploadedfile.FileName));


to this:

message.Attachments.Add(new MailAttachment(uploadedfile.FileName));


See what that gets you.
 
Share this answer
 
v2
Comments
parithi vr 18-Feb-13 5:40am    
thanks for your help.
You need to create the object of Attachment Class. The object of Attachment class can be added to MailMessage Class. Try this:
C#
SmtpClient smtpClient = null;
MailMessage smtpMail = null;
Attachment attachment = null;
try
{
    smtpMail = new MailMessage(From, To);
    smtpMail.Subject = Subject;
    smtpMail.Body = Body;
    smtpMail.IsBodyHtml = IsBodyHTML;
    attachment = new Attachment(file.Trim()); //file : Absolute path of your file
    smtpMail.Attachments.Add(attachment); // Adding attachment "System.Net.Mail"
    smtpClient = new SmtpClient();
    smtpClient.Host = "MailSettings-Host"; //Here you need to pass the host address
    smtpClient.Port = "MailSettings-Port"; //Here you need to pass the host port
    smtpClient.UseDefaultCredentials = true;
    smtpClient.Send(smtpMail);
    Response.Write("Mail sent!!");
}
catch (Exception ex)
{
    Response.Write(ex.Message);
}
finally
{
    attachment = null;
    smtpClient = null;
    smtpMail = null;
}



-Amit
 
Share this answer
 
v2
Comments
parithi vr 18-Feb-13 5:40am    
thanks for your help.
I have created a folder where my attachment will be saved. so that now the path of the file is easily get by Server.MapPath("filepath") function and then the attachment easily send to client. after sending the file it will be deleted from the folder using File.Delete(filepath) function.
 
Share this answer
 
hi dear,

Please refer my previous post on code project from below link

Send mail is Not Working when i m using Static IP?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900