简体   繁体   中英

Unable to access Outlook 2013 email sender address using c#

I've written some C# (VS2019) code to detect incoming Outlook emails and save any file attachments to a given location. This works OK, and I now have added a filter to check the source email address so that only emails from a given list of senders are processed.

My problem is that while this works fine on my laptop, it fails silently on the server when I try to access the email address of the sender. My exception handling block doesn't catch anything, and the program does not crash out.

Both machines are running Outlook 2013. The server is running Win Server 2012, and my laptop is Windows 10, 64 bit.

The program runs on the server as a scheduled console app task under a dedicated user identity. This identity is the owner of the Outlook account that the program monitors.

When running in debug on my machine, a pop up appears with wording "To prevent an unsafe abort when evaluating the function System.Reflection.Assembly.Loadfrom all threads were allowed to run...."

Popup is here

Any thoughts would be gratefully received. This is driving me mad..: :(

Thanks

Andrew

Code is as follows (I've edited out unrelated stuff, including a logging function):

public class EvtNewMail : EventArgs
{
    public EvtNewMail(MailItem email);

    public MailItem email { get; }

    public override string ToString();
}



    private static void Listener_OnNewMail(object sender, EvtNewMail e)
    {
        try
        { 
                string theSender = string.empty; 
         
                string senderName = e.email.Sender.Name;  

                string emailType = e.email.SenderEmailType.ToUpper();

                if (emailType == "SMTP")
                {
                    Log("Email type SMTP");
                    **theSender = e.email.SenderEmailAddress;** ***//FAILS HERE***
                         **//         e.email.Sender.Address Also fails**
                }
                else if (emailType == "EX")
                {
                    Log("Email type EX");
                    theSender = e.email.Sender.GetExchangeUser().PrimarySmtpAddress;
                }               

        }
        catch (System.Exception ex)
        {
            Log(Environment.MachineName + ": Error handling mail message here '")
        }

    }

What is "MessagePriority" and how is it defined? If it's part of a namespace, which one?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM