vendredi 15 juin 2018

smtp.office365.com gives net_io_connectionclosed

I've been getting some issues sending email through smtp and nobody in office seems to know what to do. I am using port 25 and I have tried 587 and 465.

C#:

public void SendMail(string from, string to, string subject, string body, 
string attachments, string pwd)
    {
    string mailServerName = 
ConfigurationManager.AppSettings["smtpServer"].ToString();
    int mailPortName = int.Parse(ConfigurationManager.AppSettings["Port"]);

    try
    {
        using (MailMessage message = new MailMessage(from, to, subject, body))
        {
            message.IsBodyHtml = true;
            SmtpClient mailClient = new SmtpClient(mailServerName, mailPortName);
            mailClient.Host = mailServerName;
            mailClient.EnableSsl = true;
            //mailClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = new NetworkCredential(from, pwd);
            mailClient.Send(message);
        }
    }
    catch (SmtpException ex)
    {
        throw ex;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

I am using smtp.office365.com as the servername and both emails passed are within our network. I have verified password and user email match fine. I've been through pretty much every link through Google and everyone is saying to use port 587 and it should work, but that isn't solving my problem in this case.

Here is the error:

Server Error in '/' Application.
Unable to read data from the transport connection: net_io_connectionclosed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Source Error: 


Line 1231:            throw ex;
Line 1232:        }
Line 1233:    }
Line 1234:   
Line 1235:}

Source File: c:\inetpub\wwwroot\ReportForm.aspx.cs    Line: 1233 

Stack Trace: 


[IOException: Unable to read data from the transport connection: net_io_connectionclosed.]
 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) +1927189
 System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) +269
 System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) +45
 System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) +79
 System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) +168
 System.Net.Mail.SmtpClient.Send(MailMessage message) +1568

 [SmtpException: Failure sending mail.]
 ReportForm.SendMail(String from, String to, String subject, String body, 
String attachments, String pwd) in c:\inetpub\wwwroot\ReportForm.aspx.cs:1233
ReportForm.imgbtnSubmit_Click(Object sender, ImageClickEventArgs e) in 
c:\inetpub\wwwroot\ReportForm.aspx.cs:1194
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +98
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +161
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981

Version Information: Microsoft .NET Framework Version:2.0.50727.8784; ASP.NET 
Version:2.0.50727.8762




Aucun commentaire:

Enregistrer un commentaire