Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
mail attaching coming failed i wrote below code
txtto.Visible = true;
lblto.Visible = true;
MailMessage msg = new MailMessage();
msg.From = "nagaswarupa@brihaspathi.com";
msg.To = txtto.Text;
msg.Subject = txtsub.Text.Trim();
msg.Body = txtbody.Text;

HttpFileCollection hfc = Request.Files;
for (int z = 0; z < hfc.Count; z++)
{
  HttpPostedFile hpf = hfc[z];
  if (hpf.ContentLength > 0)
  {
    string FileName = Server.MapPath("MyFiles") + "\\" + Path.GetFileName(hpf.FileName);
    msg.Attachments.Add(new MailAttachment(FileName));

    SmtpMail.Send(msg);
    lblmessage.Text = "Your message has been sent.";
  }
}
SmtpMail.Send(msg);
lblmessage.Text = "Your message has been sent.";

but I'm becoming error like Invalid mail attachment 'D:\crmproject\CRMProject\MyFiles\exelgridview.txt'. Where did I do a mistake?
Posted
Updated 14-Feb-11 19:56pm
v2

1 solution

Use this code
C#
MailAddress SendFrom = new MailAddress("Your Email address");
       MailAddress SendTo = new MailAddress("Your Email address");
       MailAddress MailCC = new MailAddress("Your Email address");
       MailAddress MailBCC = new MailAddress("Your Email address");


       MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
       MyMessage.CC.Add(MailCC);
       MyMessage.Bcc.Add(MailBCC);
       MyMessage.Subject = "Your subject";
       MyMessage.IsBodyHtml=true;
       MyMessage.Body = "Your body";
       Attachment attachFile = new Attachment(path+filename);

       SmtpClient emailClient = new SmtpClient("Your server IP");
       emailClient.Send(MyMessage); :)

Hope this helps!!!!!
 
Share this answer
 
v2
Comments
Rupa1 15-Feb-11 2:16am    
i have so many attachments not one sir
TejuKrishna 15-Feb-11 2:26am    
might be file size issue
Sandeep Mewara 15-Feb-11 4:00am    
Next time, use PRE tags to format code part. It makes the question readable.
Ashishmau 15-Feb-11 4:56am    
see this link
http://forums.asp.net/p/1124259/1765305.aspx
It helps......

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