首页 > 解决方案 > C#.net RESTful 服务收到带有大附件的 500 错误

问题描述

我创建了一个 Web 服务来从 Winform 应用程序发送电子邮件。数据作为 BYTE[] 发送,但仅适用于小于 3mb 的数组。在 Web 配置和 App Config 中,我将 maxAllowedContentLength 增加到了极限。这是失败的行: HttpWebResponse oWebResp = myReq.GetResponse() as HttpWebResponse;

这是 SMTP Web 服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.IO;

namespace SMTPWebService.Models
{
public class Mailer : IMailer
{
    public void SendMail(EmailContentDto emailContentDto)
    {
MailMessage mail = new MailMessage(emailContentDto.Sender, 
emailContentDto.Recipient);
        SmtpClient client = new SmtpClient();

        client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPServerPort"]);          
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Timeout = 80000;
        client.Host = ConfigurationManager.AppSettings["SMTPServerAddress"];
        mail.Subject = emailContentDto.Subject;
        mail.Body = emailContentDto.HtmlBody;
        mail.IsBodyHtml = true;

        if (emailContentDto.FileBinary != null)
            mail.Attachments.Add(
                new Attachment(
                    new MemoryStream(emailContentDto.FileBinary),
                    emailContentDto.FileName,
                    emailContentDto.FileFormat));
        try
        {
         client.Send(mail);
        }
        catch (Exception)
        {               
            throw;
        }     
    }
}
}

这是Winform处理数据:

      public void SendEmail(String sender, String recipient, String subject, String htmlBody, String fileLocation, String AttachemntNewName)
    {                        

            var stFileName = "";
            var stFileExt = "";
            byte[] buffer = null;
            String key_ = "";

            if (!String.IsNullOrEmpty(fileLocation))
            {
                FileStream st = new FileStream(fileLocation, FileMode.Open);
                stFileName = st.Name;
                stFileExt = Path.GetExtension(stFileName.ToLower());
                buffer = new byte[st.Length];
                st.Read(buffer, 0, (int)st.Length);
                st.Close();
                key_ = MimeTypesAutoGet(stFileExt);
            };

            EmailDto emailDto = new EmailDto()
            {
                Sender = sender,
                Recipient = recipient,
                Subject = subject,
                HtmlBody = htmlBody,
                FileBinary = buffer,
                FileFormat = key_,
                FileName = stFileName // AttachemntNewName
            };

            String apiUrl = "http://ADDRESS_TO_SERVER";

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(apiUrl);

            byte[] reqBytes = System.Text.UTF8Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(emailDto));

            myReq.SendChunked = true;
            myReq.AllowWriteStreamBuffering = false;

            myReq.KeepAlive = true;
            myReq.Timeout = 30000;
            myReq.Credentials = CredentialCache.DefaultCredentials;
            myReq.ContentLength = reqBytes.Length;
            myReq.Method = "POST";

            // myReq.ContentType = "multipart/form-data";    

            myReq.ContentType = "application/json";

            Stream oReqStream = myReq.GetRequestStream();

            oReqStream.Write(reqBytes, 0, reqBytes.Length);

            string StringByte = BitConverter.ToString(reqBytes);

            try
            {
                HttpWebResponse oWebResp = myReq.GetResponse() as HttpWebResponse;

            }
            catch (WebException Ex)
            {
                MessageBox.Show(Ex.ToString());
            }
    }

这是 DTO:

    public class EmailContentDto
{

    public String Recipient { get; set; }

    public String Sender { get; set; }

    public String Subject { get; set; }

    public String HtmlBody { get; set; }

    public Byte[] FileBinary { get; set; }

    public String FileFormat { get; set; }

    public String FileName { get; set; }

} 

这是控制器:

   public class HomeController : Controller
{
    private IMailer _mailer = new Mailer();

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult SendMail([FromBody]EmailContentDto emailContentDto)
    {
        _mailer.SendMail(emailContentDto);
        return null;
    }
}

标签: c#smtp

解决方案


推荐阅读