无法从服务器发送电子邮件

时间:2018-06-20 15:51:06

标签: c# email iis .net-core dotnet-httpclient

我正在尝试制作一个.net核心应用程序,该应用程序通过URL向我发送电子邮件。 它在我的本地计算机上运行完美,但是当我将其部署到服务器上时,IIS抛出了下一个异常:

  

系统存储不足。服务器响应为:4.3.1系统资源不足

当我在计算机上对其进行测试并且电子邮件发送正确时,我没有此异常,仅在IIS服务器中,我的服务器需要任何配置吗? 这是我的代码:

MailController.cs

[EnableCors("MyPolicy")]
[Produces("application/json")]
[Route("api/Mail")]
public class MailController : Controller
{
    [HttpPost("Send")]
    public string Send([FromBody]Correo mail)
    {

        try
        {
            MailMessage data = new MailMessage();

            data.Body = mail.Body;
            if (mail.BodyEncoding != null)
                data.BodyEncoding = mail.BodyEncoding;
            data.BodyTransferEncoding = mail.BodyTransferEncoding;
            data.DeliveryNotificationOptions = mail.DeliveryNotificationOptions;
            if (mail.HeadersEncoding != null)
                data.HeadersEncoding = mail.HeadersEncoding;
            data.Priority = mail.Priority;
            if (mail.SubjectEncoding != null)
                data.SubjectEncoding = mail.SubjectEncoding;
            if (mail.Headers != null)
                foreach (NameValueCollection header in mail.Headers)
                {
                    data.Headers.Add(header);
                }
            if (mail.Attachments != null)
                foreach (String att in mail.Attachments)
                {
                    Attachment attachment = new Attachment(att);
                    data.Attachments.Add(attachment);
                }
            if (mail.CC != null)
                foreach (String cc in mail.CC)
                {
                    data.CC.Add(cc);
                }
            data.From = new MailAddress(mail.From);
            data.IsBodyHtml = mail.IsBodyHtml;
            data.Subject = mail.Subject;
            foreach (String to in mail.To)
            {
                data.To.Add(to);
            }
            SmtpClient smtp = new SmtpClient("<IP of SMTP server>");
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Send(data);
            data.Dispose();
            return "Done!";
        }
        catch (Exception e)
        {
            return "Mail not send: " + e.Message;
        }
    }
}

我也通过其他任何代码从HttpClient发送此邮件...

AuthOController.cs-恢复方法

    [HttpPost("Recovery/{userName}")]
    public async Task<IActionResult> Recovery(String userName)
    {
        try
        {
          Correo sender = new Correo();
          ...
          ...
          ...
          var myContent = JsonConvert.SerializeObject(sender);
                var buffer = Encoding.UTF8.GetBytes(myContent);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                HttpClient httpClient = new HttpClient();

                var result = await httpClient.PostAsync("http://MailSender/api/Mail/Send", byteContent);

                var contents = await result.Content.ReadAsStringAsync();
               }
            return Ok("Done!");
        }
        catch (Exception e)
        {
            return BadRequest(Errors.AddErrorToModelState("recovery_failure", "There was an error in the service: " + e.Message, ModelState));
        }
    }

1 个答案:

答案 0 :(得分:0)

data.From =新的MailAddress(mail.From); 此行创建新对象,更改此 有关详细帮助,check here