ASP.NET MVC web application unable to send email v

2019-08-15 02:08发布

问题:

I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?

I am developing a web-based sales-tracking application in Visual Studio 2015 (ASP.NET MVC). The site will be hosted on an Arvixe BusinessClass for Windows shared server. The domain of the server is mydomain.com, however this domain is actually hosted by one.com, which also provides mydomain.com email.

One of the functions of the site is to inform the line manager when a user reports a sale. The line manager is to be informed via email. The email account I am trying to send from is provided by one.com.

Using System.Net.Mail.MailMessage and sending via SmtpClient, trhe following code works in my development environment (Windows 10 pro) but not deployed (deployed on Arvixe BusinessClass for Windows shared server environment):

MailMessage message = new MailMessage();
message.To.Add("recipient@mydomain.com");
message.From = new MailAddress("sender@mydomain.com", "sender@mydomain.com");
message.IsBodyHtml = true;
message.Subject = "Subject";
message.Body = "Body<br />";
SmtpClient client = new SmtpClient();
client.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "send.one.com";
client.Port = 587;
client.UseSSL = true;
client.Credentials = new NetworkCredential("sender@mydomain.com", "***");
client.Send(message);

On my Arvixe server I get the error:

    "Unable to read data from the transport connection: net_io_connectionclosed". 

Using this code I have also tried ports 465 and 25, with SSL set to true and false, and also host "mailout.one.com", without success.

I gave up and tried using MailKit with the following code:

MimeMessage message = new MimeMessage();
message.To.Add(new MailboxAddress("recipient@mydomain.com"));
message.From.Add(new MailboxAddress("sender@mydomain.com", "sender@mydomain.com"));
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "htmlbody<br />";
bodyBuilder.TextBody = "textbody";
message.Body = bodyBuilder.ToMessageBody();
message.Subject = "Subject";

using (var client = new SmtpClient())
{
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("send.one.com", 587, MailKit.Security.SecureSocketOptions.StartTls);  
    client.Authenticate("sender@mydomain.com", "***");
    client.Send(message);
    client.Disconnect(true);
}

Again it works in my development environment, but not in production. Here I get the error:

    "The operation is not allowed on non-connected sockets".

Of note, both solutions work in deployed environment if I change the host to 127.0.0.1 and add an example@mydomain.com email account and connected user account in Arvixe, but only for external email addresses. Addresses ending in mydomain.com obviously get rerouted back to the server itself, as the server's domain is mydomain.com, but as the email provider is one.com and not the Arvixe server, the email doesn't get through.

Other things I have tried without success:

-Setting the SPF record for mydomain.com to include my server address, and my server IP address.

-Using an Office365 account on a different domain to send from (host "smtp.office365.com")

I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?

Arvixe/One.com suggestions (from support tickets and live chat):

  1. SMTP Authentication must be used.
  2. The local mail server, or localhost must be used to send email via web script. (try xxx.win.arvixe.com, localhost, or 127.0.0.1 - port 25 or 26, or xxxsecuremail.win.arvixe.com - port 465 for secure mail)
  3. The FROM email address, and SMTP Authentication email address, must be a local user on the local server.

If you have your own remote email server and intend to send emails via web script to email addresses that are local to your domain, then that mail domain needs to be removed from the local server (we have to do this) and you will have to use an account from another domain local to the server to send mail from. Otherwise any email going to the local domain will be delivered locally and not to your remote server.