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

これらのファイルを送信する別の方法を教えていただければ、それも素晴らしいことです

4

1 に答える 1

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

何か違うものを見たいとおっしゃいました...添付コードは問題ないようですので、添付ファイルとしてではなく、電子メールにインラインで画像を埋め込むことができるコードをいくつか提供したいと思いました:

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

キャッチされた特定のエラーメッセージ/例外を投稿すると、より多くのヘルプが得られます...

于 2010-07-10T01:30:29.137 に答える