locked
How can I programatically send email via Outlook? RRS feed

  • Question

  • Hi all;

    What I want to do is similar to this question. However that solution uses Settings.Default.* which as best as I can tell doesn't exist.

    For a Windows Forms (C#) app, how can I send an email usign the local email client and credentials so I don't have to prompt the user for anything. I definitely don't want to ask the user for their password (and more to the point then have to save that as this will be for a service sending scheduled emails).

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Sunday, March 25, 2018 11:11 PM

All replies

  • Hi DavidThi808,

    Thank you for posting here.

    >>However that solution uses Settings.Default.* which as best as I can tell doesn't exist.

    For your question, Settings.Default.* is Application Settings.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings

    You could set it by following the steps below.

    Right click the project> Properties> Settings

    The way you want to used is also need password. It just stores in Settings without showing in the code.

    Here are some code samples about how to send mail via outlook for your reference.

    https://www.codeproject.com/Tips/165548/C-Code-snippet-to-send-an-Email-with-attachment-fr

    https://msdn.microsoft.com/en-us/library/office/bb644320.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 26, 2018 7:43 AM
  • Monday, March 26, 2018 8:27 AM
  • Unfortunately that still requires my knowing their email server host name. And I don't know it (this is for a commercial app).

    Is there a way to do this where I don't know that? Where the host name is somewhere in some system setting?

    Especially for the case where someone is on hosted Exchange.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Monday, March 26, 2018 11:34 AM
  • You can get SMTP servers from Outlook Profiles with interfaces like IProfAdmin, IOlkAccountManager

    (but I only tested them in C++...)

    Monday, March 26, 2018 1:51 PM
  • That's not Outlook. And you don't get your sent mail in "Sent Items" folder when sending in this way.

    EDIT: Okay, I read the solution he quoted and that doesn't involve Outlook, so perhaps that's not important after all.


    Wednesday, March 28, 2018 1:27 AM
    Answerer
  • Hi cheong00;

    What I'd prefer is some way that it uses Outlook so I don't have to ask them for their mail server and credentials. I definitely don't want to be storing their password.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Thursday, April 12, 2018 3:35 PM
  • I remember that because of a few previous virus outbreak that use Outlook as spread vector, whenever you use COM interface to automate Outlook to send email, it'll show a popup for confirmation and has provided no way to disable it.

    If your program runs in environment with Active Directory, the best solution is to query the MX record of current DNS suffix, then use default credential (i.e.: the identity of current user) to send mail via that SMTP server.

    If you application does not run in in-house environment, there's no guarantee that it would work. So if that fails, the second thing is to try send mail via Outlook and accept that the user have to click that dialog away to send the email. 

    If that also fails, I guess the only option left is to ask the user what the mail settings are.


    Thursday, April 12, 2018 4:42 PM
    Answerer