Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
 
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 11-Feb-13 19:25pm
Comments
Guirec Le Bars at 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 at 18-Feb-13 5:50am
   
This is repeated question, please have a look if it already exists before posting..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
parithi 2000 at 18-Feb-13 5:40am
   
thanks for your help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You need to create the object of Attachment Class. The object of Attachment class can be added to MailMessage Class. Try this:
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
  Permalink  
v2
Comments
parithi 2000 at 18-Feb-13 5:40am
   
thanks for your help.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi dear,
 
Please refer my previous post on code project from below link
 
Send mail is Not Working when i m using Static IP?[^]
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100