C#.net RESTful服务因大附件而出现500错误

时间:2018-05-23 12:54:21

标签: c# smtp

我创建了一个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;
        }     
    }
}
}

以下是Wform数据的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;
    }
}

0 个答案:

没有答案