c# - 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;
}
}
解决方案
推荐阅读
- redis - Redis 集群的 RedisSearch 模块
- c++ - 除了 FD=0,1,2 之外,还有什么方法可以安全成功地写入手动选择的文件描述符编号?
- html - 为什么我的烧瓶应用程序顶部有空间
- blazor - 在 MainLayout.razor 或 App.razor 中添加 MatBlazor MatThemeProvider
- python - 用 BeautifulSoup 显示 XML 树结构
- python - 如何解决错误画布和GameWidget
- pandas - 我怎样才能从这里的数据中获得带有正确数字的表格?
- javascript - 任务:使用 for 循环遍历数组并向所有用户添加 10 个积分
- python - ndb 云数据存储写入默认命名空间
- java - 添加方法时我的java输出出现两次?