0

I would like to add email functionality to a WinForm program I'm writing in C#. I have an Android app that has email functionality. What it does is set up the email but then lets the user choose the email program, etc. Once that is chosen the email body is completed. But it's up to the use to select what email app they want to use.

I would like to do the same in Windows but I don't see how. I have tried the following (based on other questions and responses here) :

        _from = new MailAddress("my email address", "xxxx");
        _to = new MailAddress("xxxx3333@gmail.com", "yyyy");
        SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
        smtp.UseDefaultCredentials = true;
        smtp.Port = 587; 
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp msgMail = new MailMessage();
        smtp.Body = text; 
        msgMail.Subject = "Subject";
        msgMail.From = _from;
        msgMail.To.Add(_to); 
        smtp.EnableSsl = true;
        msgMail.Subject = _subject;
        msgMail.Body = Text;
        msgMail.IsBodyHtml = false;
        try
        {
            mailClient.Send(msgMail);
        }
        catch (Exception ex)
        {
            string msg = "Exception caught in sending the email: " + ex.ToString();
            showMessage(msg);
        }

        msgMail.Dispose();

But I get:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

With similar code in Android, my program just gets to an email form but lets the user decide what email add they will use.

Is there a way to do this in Windows?

There is an almost identical question and response here:

C# Windows Form Application - Send email using gmail smtp

And I think I've followed this but...doesn't work.

Ron
  • 2,435
  • 3
  • 25
  • 34
  • Related : https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – Phat Huynh Sep 19 '19 at 04:15
  • 1
    Possible duplicate of [Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required](https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) – Mahesh Waghmare Sep 19 '19 at 04:16
  • It is somewhat similar to the article referenced just above but that concerns problems with a production server. This is from a home client. But what I would like is to simply have a form like in Android where the app fills in some fields (for me, the message) and let the user decide the rest. There the user selects the _to addresses and the app fills in the message and the user decides the app to use to send, so no need for my app to know about which port to use or what kind of authentication, etc. – Ron Sep 19 '19 at 04:35

3 Answers3

2

To directly answer your question - you probably haven't enabled less secure apps on the gmail account you are using.


Otherwise though, you could investigate the syntax of mailto if you want to let the user elect a mail client to use to send the email: https://www.labnol.org/internet/email/learn-mailto-syntax/6748/

From the link:

Send an email to Barack Obama with the subject “Congrats Obama” and some text in the body of the email message

<a href=”mailto:obama@whitehouse.gov? 
subject=Congrats%20Obama&body=Enjoy%20your%20stay%0ARegards%20″>

This isn't directly related to C#/Windows - but I do know entering mailto:someone@somewhere.com at the Run prompt works:

enter image description here

Presumably then you could do something like: (untested)

Process.Run("mailto:someone@somewhere.com");
Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
1

From the server response messages it looks like you have to provide login credentials before you are allowed to send.

Replace:

smtp.UseDefaultCredentials = true;

With:

smtp.Credentials = new System.Net.NetworkCredential("yourusername", "yourpassword");

This should do the trick.

gwinnem
  • 66
  • 4
  • As I sort of mentioned above, the better solution would be to keep the app ignorant of a particular user's credentials. If I could just get a form that would simply add the relevant data (the message) and let the user decide the rest. – Ron Sep 19 '19 at 04:46
0

You may have forgotten in your code to add the Host

Try to use this :

SmtpClient smtp = new SmtpClient();

smtp.UseDefaultCredentials = true;

smtp.Host = "SRVMAIL";

mlamsarf
  • 26
  • 6