0

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

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • Can you give a bit more background on what you are trying to do? As I read it, you want to silently manipulate the local email client and send an email. But, there are a ton of unknowns here. Do you know what email client the computers will be using? My computer doesn't have the local email client configured for security reasons for example. If it is for a service to send scheduled emails, why does it need to connect to the local email client instead of an SMTP server? – UnhandledExcepSean Mar 27 '18 at 17:26
  • Fundamentally I want to send email without asking the user for any information on their email (server, login) and definitely without my holding their password. – David Thielen Mar 27 '18 at 21:04
  • The the email needs to look like it came from their personal account? – UnhandledExcepSean Mar 27 '18 at 21:34
  • @UnhandledExcepSean - It should look like it's from them. They're the ones sending it from the app I'm writing. But that's not an absolute requirement. – David Thielen Mar 27 '18 at 23:00
  • Are these computers all on your network or are they public? – UnhandledExcepSean Mar 27 '18 at 23:18

1 Answers1

0

You could add the below configuration in your App.config

<system.net>
<mailSettings>
  <smtp from="donotreply@yourCompany.com" deliveryMethod="Network">
    <network host="mail.yourCompany.com" userName="sender@yourCompany.com" password="yourPassword" port="26" />
  </smtp>
</mailSettings>
</system.net>

Then, in the Send method, use parameter less constructor of SMTP client. Your Send method will become something like this

 public void Send(IEnumerable<string> to, string subject, string message)
    {
        var client = new SmtpClient(); 
        var mailMessage = new MailMessage();
        mailMessage.IsBodyHtml = true;
        mailMessage.Bcc.AddRange(to.Select(email => new MailAddress(email)));          
        mailMessage.Subject = subject;
        mailMessage.Body = message;
        client.Send(mailMessage);
    }

If storing password in configuration is a concern, then you can simply encrypt it and use a decrypter in your application code.

Saad
  • 198
  • 2
  • 12