2

I'm sending emails from my winforms app by using the MailMessage functionality.

I compile the email, save it to disk and then the default mail client will open the mail for the user to verify the settings before hitting Send.

I want to set the 'From' of the email automatically to the default email account as set up in the mail client. How can I do that?

var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromEmailAccount);
mailMessage.To.Add(new MailAddress("recipient@work.com"));
mailMessage.Subject = "Mail Subject";
mailMessage.Body = "Mail Body";

If I leave fromEmailAccount blank I get an error, and if I set it to something like 'test@test.com' the email does not send as the local account does not have permissions to send it via an unknown account.

Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77
  • see this http://stackoverflow.com/questions/809538/how-to-send-email-using-default-email-client – prem Aug 06 '15 at 08:36
  • I have exactly this problem, and as far as I can tell there isn't a solution which will work for users on any operating system with any smtp client. – Philip Stratford Oct 05 '17 at 11:41

2 Answers2

1

Get the user information from the OS (If windows 8, 10) them set the from to the user email, see this question: Get user information in Windows 8?

Community
  • 1
  • 1
Mbithy Mbithy
  • 138
  • 13
1

I have tested the following code and this seems to grab the local default mail from address and launches the mail message window, hope it helps:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace testMailEmailUser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Outlook.Application application = new Outlook.Application();
            Outlook.AddressEntry mailSender = null;

            Outlook.Accounts accounts = application.Session.Accounts;

            foreach (Outlook.Account account in accounts)
            {
                mailSender = account.CurrentUser.AddressEntry;
            }

            Outlook.MailItem mail =
                application.CreateItem(
                Outlook.OlItemType.olMailItem)
                as Outlook.MailItem;

            if (mailSender != null)
            {
                mail.To = "someone@example.com; another@example.com";
                mail.Subject = "Some Subject Matter";
                mail.Body = "Some Body Text";
                mail.Sender = mailSender;
                mail.Display(false);
            }
        }

    }
}
Psymon25
  • 336
  • 2
  • 18
  • Sadly, I can't use this solution for my ASP.NET application, and it's obviously no use if the user is not using Windows and Outlook. – Philip Stratford Oct 05 '17 at 11:42