Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义内容类型发送SMTP电子邮件_C#_.net - Fatal编程技术网

C# 使用自定义内容类型发送SMTP电子邮件

C# 使用自定义内容类型发送SMTP电子邮件,c#,.net,C#,.net,我正在用.NET SmtpClient和附件文件发送电子邮件。 文件扩展名为“.xls”,我需要将其与一起发送 内容类型:应用程序/excel; 但它是用 内容类型:application/vnd.ms-excel 尝试使用指定的设置内容类型创建AttacheTnt,但没有效果 附件数据=新附件(fileStream,新ContentType(“application/excel;name=asd.xls”) 同样,通过这种方式: 附件数据=新附件(fileStream、“file.xls”、“

我正在用.NET SmtpClient和附件文件发送电子邮件。 文件扩展名为“.xls”,我需要将其与一起发送 内容类型:应用程序/excel; 但它是用 内容类型:application/vnd.ms-excel

尝试使用指定的设置内容类型创建AttacheTnt,但没有效果

附件数据=新附件(fileStream,新ContentType(“application/excel;name=asd.xls”)

同样,通过这种方式: 附件数据=新附件(fileStream、“file.xls”、“application/excel”)

是我的代码错误还是SMTP服务器以某种方式覆盖了内容类型?? (我已试用了3台不同的网络服务器)

excel文件的正确内容类型为application/vnd.ms-excel,请尝试使用此代码

 MailAddress fromAddress = new MailAddress("aaa@interia.pl", "Tom S");
        MailAddress toAddress = new MailAddress("bbb@interia.pl", "John B");
        const string fromPassword = "my_pass";
        string body = "new message";

        using (SmtpClient smtp = new SmtpClient
        {
            Host = "poczta.interia.pl",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        })
        {
            using (MailMessage message = new MailMessage(fromAddress, toAddress))
            {
                message.Subject = Environment.MachineName;
                message.Body = body;

                using (var fileStream = File.OpenRead(@"E:\ExcellOld.xls"))
                {
                    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("application/vnd.ms-excel");
                    System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(fileStream, ct);
                    message.Attachments.Add(attach);
                    smtp.Send(message);
                }
            }
        }

是的,我知道,但我需要将其与“应用程序/excel”内容类型一起发送,以便客户端系统识别最好的方法是将客户端中的此值从应用程序/excel更改为新值,但它不可能是客户端它的旧应用程序,没有机会更改:(有什么想法吗?删除System.Net.Mime.ContentType代码,并将附件代码更改为System.Net.Mail.attachment attach=新的System.Net.Mail.attachment(fileStream,“file.xls”,“application/excel”);请试试这个。例如,我发布的代码示例我正在使用System.Net.Mail.Attachment thereHTTP,您不能直接发送excel之类的二进制文件。如果您查看下面的Pedro解决方案,他使用的是System.Net.Mime.ContentType,它正在将二进制文件转换为64进制字符串。
 MailAddress fromAddress = new MailAddress("aaa@interia.pl", "Tom S");
        MailAddress toAddress = new MailAddress("bbb@interia.pl", "John B");
        const string fromPassword = "my_pass";
        string body = "new message";

        using (SmtpClient smtp = new SmtpClient
        {
            Host = "poczta.interia.pl",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        })
        {
            using (MailMessage message = new MailMessage(fromAddress, toAddress))
            {
                message.Subject = Environment.MachineName;
                message.Body = body;

                using (var fileStream = File.OpenRead(@"E:\ExcellOld.xls"))
                {
                    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("application/vnd.ms-excel");
                    System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(fileStream, ct);
                    message.Attachments.Add(attach);
                    smtp.Send(message);
                }
            }
        }