C#从Outlook中的所有帐户检查SmtpAddress

时间:2018-04-27 13:36:39

标签: c# email outlook

我正在尝试通过C#和Outlook发送电子邮件。它有时确实起作用了,而且从来没有真正的代码是皮疹是这个

trim

在If子句中,它始终为false,即使它必须是正确的。我手动检查了一下。似乎无法创建来自帐户帐户 ...

错误的Stacktrace:

    Menu customMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.featured, menu);
    customMenu = updateCart(menu, "0");

    return super.onCreateOptionsMenu(customMenu);
}


public Menu updateCart(Menu customMenu, String count) {

    MenuItem item = customMenu.findItem(R.id.action_cart);
    MenuItemCompat.setActionView(item, R.layout.layout_cart_count);
    FrameLayout notifCount = (FrameLayout) MenuItemCompat.getActionView(item);

    TextView tv = (TextView) notifCount.findViewById(R.id.actionbar_notifcation_textview);
    tv.setText(count);

    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Toast.makeText(getApplicationContext(),
                    "action_cart : selected", Toast.LENGTH_LONG)
                    .show();
        }
    });
    return customMenu;
}

和例外消息:

            //Look for our account in the Outlook
            foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
            {
                if (account.SmtpAddress.Equals(sFromAddress, StringComparison.CurrentCultureIgnoreCase))
                {
                    //Use it
                    acc = account;
                    break;
                }
            }

对于进一步的代码,这里是要点链接:https://gist.github.com/1524045patrick/400b3676c0e95627334a09ba9cc39c2e

1 个答案:

答案 0 :(得分:1)

如果帐户没有SMTP地址,SmtpAddress将返回一个空字符串。

我建议检查Account.AccountType属性,该属性在OlAccountType枚举中返回一个常量,指示Account的类型。

如果是olExchange帐户,您需要通过以下方式获取SMTP地址:

    Outlook.AddressEntry sender =
        account.CurrentUser.AddressEntry;
    if (sender != null)
    {
        //Now we have an AddressEntry representing the Sender
        if (sender.AddressEntryUserType ==
            Outlook.OlAddressEntryUserType.
            olExchangeUserAddressEntry
            || sender.AddressEntryUserType ==
            Outlook.OlAddressEntryUserType.
            olExchangeRemoteUserAddressEntry)
        {
            //Use the ExchangeUser object PrimarySMTPAddress
            Outlook.ExchangeUser exchUser =
                sender.GetExchangeUser();
            if (exchUser != null)
            {
                return exchUser.PrimarySmtpAddress;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return sender.PropertyAccessor.GetProperty(
                PR_SMTP_ADDRESS) as string;
        }
    }
    else
    {
        return null;
    }