通过Gmail发送附件不能使用某些类型 - 为什么?

时间:2010-07-09 22:01:13

标签: c# email-attachments

为什么我不能发送xls,doc和其他文件 - 它适用于jpg,txt和其他文件。

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("mymail@gmail.com");
            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 = "mymail@gmail.com";//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);
            }
        }

如果你能告诉我发送这些文件的另一种方式,那么它也会很棒

1 个答案:

答案 0 :(得分:1)

如果您的jpeg和文本文件正在运行,我猜你的问题可能在你的一些其他文件类型的路径中或者其他一些文件的大小(只是一个疯狂的猜测,就像你发布的代码一样)看起来不错。)

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

以下是一些工作代码的片段。请注意,我从未明确设置mm.BodyEncoding = System.Text.Encoding.UTF8;mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;属性并取得了成功。 (可能只是一个无关的观察......)

  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"));

你提到你想要看到不同的东西......好吧,你的附件代码看起来很好所以我想我会提供一些代码,允许你在你的电子邮件中内嵌图像,而不是作为附件: / p>

// 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);

发布一些特定的错误消息/异常,您将获得更多帮助......