views:

48

answers:

1

Why can't I send xls,doc and other files - it does work for jpg,txt and others.

private void BuildAndSend(string pTo,string pCC,string pSubject,string pBody)
        {
            // building the mail
            System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress(pTo);

            System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("[email protected]");
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);
            mm.Subject = pSubject ;
            mm.Body = pBody;

            System.Net.Mail.MailAddress cc = new System.Net.Mail.MailAddress(pCC);
            mm.CC.Add(cc);

            addAttachments(mm);
            mm.IsBodyHtml = true;
            mm.BodyEncoding = System.Text.Encoding.UTF8;

            //sending the mail
            sendMail(mm);
        }

        private void addAttachments(System.Net.Mail.MailMessage mm)
        {
            string attachmentFile;
            for (int i = 0; i < lstAttachments.Items.Count ; i++)
            {

                string fileFullName = pullDictionary[i];
                attachmentFile = fileFullName;
                System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(attachmentFile);
                mm.Attachments.Add(mailAttachment);

            }

        }

        private void sendMail(System.Net.Mail.MailMessage mm)
        {
            try
            {
                // loging in into sending user account
                string smtpHost = "smtp.gmail.com";
                string userName = "[email protected]";//sending Id
                string password = "mypass";
                System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();
                mClient.Port = 587;
                mClient.EnableSsl = true;
                mClient.UseDefaultCredentials = false;
                mClient.Credentials = new NetworkCredential(userName, password);
                mClient.Host = smtpHost;
                mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                mClient.Send(mm);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

if you can show me another way to send these files it will be great as well

+1  A: 

If your jpegs and text files are going I'm guessing your problem may be in your path to some of the other file types or in the size of some of these other files (just a wild guess really as the code you posted looks ok).

// this looks suspect
string fileFullName = pullDictionary[i];
attachmentFile = fileFullName;

Here is a snippet of some working code. Note that I've never set either the mm.BodyEncoding = System.Text.Encoding.UTF8; or mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; properties explicitly and have had success. (Probably just an unrelated observation...)

  MailMessage m = new MailMessage(_gmailEmail, _to);
  m.Subject = _emailSubject;
  m.Body = _body;
  for (int i = 0; i < lstAttachments.Items.Count ; i++) // your list
    m.Attachments.Add(new Attachment("\path\to\file\to\attach\here"));

You mentioned that you'd like to see something different... Well, your attachment code looks fine so I thought I'd provide some code that allows you to embed images inline in your email rather than as an attachment:

// the below adds embedded images an email...
  AlternateView avHtml = AlternateView.CreateAlternateViewFromString(
      _body, null, System.Net.Mime.MediaTypeNames.Text.Html);
  LinkedResource pic = new LinkedResource("\path\to\file\to\embed\here", System.Net.Mime.MediaTypeNames.Image.Jpeg);
  pic.ContentId = "IMAGE1"; // just make sure this is a unique string if you have > 1
  avHtml.LinkedResources.Add(pic);
  m.AlternateViews.Add(avHtml);

Post some specific error messages/exceptions caught and you'll get more help...

Tahbaza
Works like a charm... It was a little project for my mom which I built six month ago. After a while I just gave up (All she care about in emails are the attachments).Now, when I need email functionality again... I finally got it from you and it's funny that I'm not only thankful for helping me, but I'm also excited because I would be able to give my mom the gift I wanted to give her before...So since it become personal... you got a big genuine thank you from the bottom of my heart :)
Asaf