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

发布于 2024-09-09 01:18:02 字数 2539 浏览 6 评论 0原文

为什么我不能发送 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("[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);
            }
        }

如果您能告诉我另一种发送这些文件的方法,那就太好了

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-09-16 01:18:02

如果您的 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"));

您提到您希望看到不同的东西...好吧,您的附件代码看起来不错,所以我想我会提供一些代码,允许您在电子邮件中嵌入内联图像而不是作为附件:

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

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

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文