none
non-helpful exception flagged on _MailItem.Send() RRS feed

  • Question

  •   Outlook.Application outlook_app = new Outlook.Application();

      Outlook.Account account = GetAccountForEmailAddress(outlook_app, from_address);

    Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem); newMail.To = to_address; newMail.Subject = email_subject; newMail.Body = body; newMail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain; newMail.SendUsingAccount = account; try { newMail.Send(); //green underline is okay } catch (Exception e) { email_error = e.ToString(); MessageBox.Show(email_error); }

            public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string emailAddress)
            {
                // Loop over the Accounts collection of the current Outlook session.
                Outlook.Accounts accounts = application.Session.Accounts;
                foreach (Outlook.Account account in accounts)
                {
                    // When the e-mail address matches, return the account.
                    if (account.SmtpAddress.ToLower() == emailAddress.ToLower())
                        return account;
                }
                MessageBox.Show("No account with email address: " + emailAddress + " exists!");
                return null;
            }

    The code executes to the .Send then gets this exception

    System.ArgumentException: Sorry, something went wrong. 
    at Microsoft.Office.Inerop.Outlook._MailItem.Send()

    I've poked around with other settings of the MailItem but nothing jumps out esp. since this code was taken from an MSDN example.

    The account that is matched is an Outlook email address linked to Office 365.


    David K. Ream

    Friday, March 17, 2017 2:32 PM

Answers

  • So is that an IMAP4 account? What happens if you replace Send() with Save() and then manually open the saved message and try to send it?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Dave Ream Friday, March 17, 2017 9:06 PM
    Friday, March 17, 2017 8:22 PM
  • so I finally got it to work by changing the To address I was trying to use. I've tried several other email address and they work fine. the one I was trying to use at first is a legal email address so I have no idea why it was "rejected".

    David K. Ream

    • Marked as answer by Dave Ream Saturday, March 18, 2017 4:27 AM
    Saturday, March 18, 2017 4:27 AM

All replies

  • What kind o message store is it? Does it work if you don't set the SendUsingAccount property?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 17, 2017 2:37 PM
  • if I comment out the SendUsingAccount line, it fails with the same message.

    My "local" Outlook does have multiple email accounts FYI.


    David K. Ream

    Friday, March 17, 2017 2:43 PM
  • So what kind of store is it? Cached Exchange? PST? Google Apps?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 17, 2017 2:50 PM
  • I don't understand what you are asking. by store do you mean what type of email software? Outlook client with the account being Office 365.

    David K. Ream

    Friday, March 17, 2017 2:52 PM
  • The store is where the folders and messages reside. So it sounds like in your case it is a cached Exchange store (OST), right?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 17, 2017 3:04 PM
  • yes there's an OST file in a folder on my local PC.

    David K. Ream

    Friday, March 17, 2017 3:15 PM
  • Hello Dave,

    To avoid the following issue with underline:

    newMail.Send(); //green underline is okay
    

    you need to define a mail item in the code as following:

    Outlook._MailItem newMail = (Outlook._MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
    

    Do you get the same results with an SMTP account? Did you have a chance to create a new profile in Outlook? Does it work correctly?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Friday, March 17, 2017 4:39 PM
  • using _ in front of the MailItem(s) in the line you note still gave the same error.

    I don't have an SMTP account to test on. Although in the code that finds the account to use, I do see an email address value in the account.SmtpAddress property not smtp.something.com. I assumed that was because the account is IMAP "protocol".


    David K. Ream

    Friday, March 17, 2017 8:18 PM
  • So is that an IMAP4 account? What happens if you replace Send() with Save() and then manually open the saved message and try to send it?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    • Marked as answer by Dave Ream Friday, March 17, 2017 9:06 PM
    Friday, March 17, 2017 8:22 PM
  • now we are getting close. Save executed without error and put the message into the drafts folder.

    I'd like to go into the outbox as the app is trying to auto-generate messages that just need to be sent.

    I'm will to hit the Send button manually but don't want to open each one manually and hit the each message's Send button manually.


    David K. Ream

    Friday, March 17, 2017 8:26 PM
  • You can use the Move method to place the newly-created item in the Drafts folder. 

    Also you can create a new item by using the Add method of the Items class which comes from the Drafts folder. In that case the new item will be saved to the Drafts folder automatically. See How to create and show a new Outlook mail item programmatically: C#, VB.NET .

    Also you may find the How To: Create and send an Outlook message programmatically article helpful. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Friday, March 17, 2017 9:02 PM
  • I got the Move to work and the email ends up in the Outbox.

    But when I send messages, it just sits there and is not sent.

    If I manually drag to Drafts, open it, hit Send and then Send emails, it does get sent.

    There must be some property or flag I have to set so it will be sent without the manual steps?


    David K. Ream

    Friday, March 17, 2017 9:51 PM
  • Outbox is just an eye candy - moving a message there does nothing.

    Again, what happens if you manually open the message and click Send?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 17, 2017 10:01 PM
  • As I noted in my previous post, opening and sending the email does get it to be sent.

    So my code now looks like this

    Outlook.MAPIFolder outbox = (Outlook.MAPIFolder)account.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

    Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem); newMail.To = to_address; newMail.Subject = email_subject; newMail.Body = body; newMail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain; try { newMail.Save(); newMail.Move(outbox); }

    if I add newMail.Send(), I'm back to getting the "something went wrong message".


    David K. Ream

    Friday, March 17, 2017 10:06 PM
  • What happen if you create a message manually and save it? Can you then programmatically open the message using Nmaepace.GetItemFromID and call MailItem.Send?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, March 17, 2017 10:11 PM
  • Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem);
       newMail.To = to_address;
       newMail.Subject = email_subject;
       newMail.Body = body;
       newMail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
       newMail.Sender = account.CurrentUser.AddressEntry;
       newMail.SendUsingAccount = account;
    
       try
       {
          newMail.Save();
          newMail.Send();
       }
    

    Here's the way the code looks at the moment. If I set a breakpoint on the .Send(), then open the email saved to the Drafts folder, I can see the expected To address. Then I close the email. Then I execute the .Send() which gives the error noted previously, but if I look at the email in the Drafts folder the To address has been cleared out now.

    That seems odd. 


    David K. Ream

    Saturday, March 18, 2017 4:17 AM
  • so I finally got it to work by changing the To address I was trying to use. I've tried several other email address and they work fine. the one I was trying to use at first is a legal email address so I have no idea why it was "rejected".

    David K. Ream

    • Marked as answer by Dave Ream Saturday, March 18, 2017 4:27 AM
    Saturday, March 18, 2017 4:27 AM