Generating an email using either AutodiscoverURL or SmtpClient

126 views Asked by At

I am generating an email through my MVC application however I am receiving different results between my development environment and test/production environment. I have tried implementing using AutodiscoverUrl() and SmtpClient(). We are using an IIS Server local to my computer for development and on the server for test/production.

With the SmtpClient.

  • in development

    • sending to emails with addresses within our domain are working as intended, receive notification of delivery

    • sending to emails like @gmail.com and @outlook.com the sent from is correct but @gmail goes straight to the spam folder and @outlokk goes to the in box, sometimes receive notification or delivery complete

  • in test/deployment
    • sending to emails with addresses within our domain are working as intended, no notification of delivery
    • sending to emails like @gmail.com and @outlook.com the sent from is correct but it goes straight to the spam folder, no notification or delivery complete

with AutodiscoverUrl()

  • in development
    • sending to emails with addresses within our domain the sent from is listed as my email on behalf as the email I want it to be from, the sent email ant read receipt is in my email box
    • sending email to @gmail.com the sent from is correct, the sent email is in my sent folder and there is no read receipt
    • sending to @outlook.com the from is correct, the sent email is in my email sent folder, and the read receipt goes to my email box with the "To" blank in the receipt (who the original email went to)
  • in test/deployment
    • to email address with same domain the sent from is correct however there is no read receipt and no item in any sent folder
    • to @gmail.com the sent from is correct however it shows up in the spam folder and takes forever to show up, no read receipt and no item in sent folder
    • to @outlook.com the sent from is correct however it is delivered to the junk mail folder, no read receipt and no item in sent folder

The code I used is --

 using (SmtpClient sender = new SmtpClient("mail.state.gov"))
        {
            using (MailMessage mailmessage = new MailMessage())
            {
                mailmessage.To.Add(address.Email);
                mailmessage.From = (new MailAddress("[email protected]"));
                mailmessage.Subject = subjectline;
                mailmessage.Body = Body(personID);
                mailmessage.Priority = MailPriority.Normal;
                mailmessage.IsBodyHtml = true;
                mailmessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure |
   DeliveryNotificationOptions.OnSuccess;
                mailmessage.Bcc.Add("[email protected]"); 

                sender.Send(mailmessage);
            }
        }

And then I tried

        string from = "[email protected]";
        ExchangeService service = new ExchangeService();
        service.AutodiscoverUrl(from);

        EmailMessage message = new EmailMessage(service);
        message.Subject = subjectline;
        message.Body = Body(personID);
        message.ToRecipients.Add(address.Email);
        message.From = from;
        message.IsReadReceiptRequested = true;
        message.Save();

        message.SendAndSaveCopy();
1

There are 1 answers

0
Xavier J On

The Exchange approach depends on the currently logged-in user. On your workstation, that's always going to be you. On a "real" IIS server, it's going to be whatever service account the site is configured to execute as. You don't have complete control of what shows up in the "From" address when it's received, which is why that "on behalf of" stuff is showing up.

I presume that if you are sending to Exchange from Exchange, on the same domain, then the messages aren't ever leaving your intranet so they wouldn't be assumed to be spam. But for the stuff going through SMTP, it's going through the spam filters on Gmail or Outlook. You can't configure the spam filters on those systems. Apparently whatever e-mail you're sending out is tripping the filters. Modify the e-mail. You might look at articles like this so that you can improve the content, but recognize that ultimately you have no control.