无法从表单发送word文档(.doc和.docx)作为电子邮件附件

时间:2014-12-16 12:53:36

标签: c# asp.net

我试图发送一封包含表单附件的电子邮件给自己。当我将.pdf文件添加为附件时,这会成功运行。但是当我尝试.doc或.docx文件时它失败了。

我在javascript中添加了一些验证,但它接受我需要的每种文件类型:

var validFileTypes = ["doc", "docx", "pdf", "tiff"];

这是我的 c#代码,用于检查我的表单并发送电子邮件:

private bool SendJobMail()
    {
        var client = new MailerServiceReference.IemailClient();
        var succeeded = false;

        string _recipient = "jobs@company.eu";
        string _firstName = firstName.Value;
        string _lastName = lastName.Value;
        string _telNr = phone.Value;
        string _motivation = motivation.Value;
        string _sender = "info@company.eu";


        int vacId = Int32.Parse(vacancyId.Value);
        string vacTitle = _vacancies.Find(vac => vac.Id == vacId).Title;
        string subject = string.Format("Application from {0} {1} for {2}", _firstName, _lastName, vacTitle);

        _motivation += string.Format("\n\n{0}{1}\n{2}\n{3}", _firstName, _lastName, email.Value, _telNr);

        //set up and add an attachment
        string attachmentFilename = Path.GetFileName(cv.PostedFile.FileName);
        Attachment attachment = new Attachment(cv.FileContent, attachmentFilename, MediaTypeNames.Application.Octet); //can interpret PDF and richtextdocument

        ContentDisposition disposition = attachment.ContentDisposition; //disposition necessary for some mailing applications
        disposition.CreationDate = File.GetCreationTime(attachmentFilename);
        disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
        disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
        disposition.FileName = Path.GetFileName(attachmentFilename);
        disposition.Size = cv.PostedFile.ContentLength;
        disposition.DispositionType = DispositionTypeNames.Attachment;

        try
        {
            client.Open();
            client.SendEmail("CompanyPublicSite", _sender, _recipient, subject, _motivation);
            succeeded = true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            client.Close();
        }

        return succeeded;
    }

如果您需要任何其他代码,请与我们联系。

0 个答案:

没有答案