Contact Us form

Eduardo Gomez 3,416 Reputation points
2023-01-23T22:52:02.8166667+00:00

Hello

I am creating a contact us form

User's image

The idea here is that people contact, the developer (me), with a bug or future request

So, the user will type his/her email, and some comments will be sent to my personal email address.

private static void SendEmail(RichTextBox richTextBox, TextBox textBox) {
 
           TextRange textRange = new(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
 
           try {
               string fromAddress = textBox.Text;
               MailMessage mail = new()
               {
                   From = new MailAddress(fromAddress)
               };
               mail.To.Add("rgomezr@outlook.com");
               mail.Subject = "Transcribed";
               mail.Body = textRange.Text;
 
               SmtpClient SmtpServer = new("smtp-mail.outlook.com")
               {
                   Port = 587,
                   EnableSsl = true,
                   Credentials = new System.Net.NetworkCredential("my personal email@outlook.com", "my app password") // since I have 2 fator auth, I must use an app password
               };
 
               SmtpServer.Send(mail);
               MessageBox.Show("Email sent successfully.");
 
               SmtpServer.Send(mail);
               MessageBox.Show("Email sent successfully.");
           } catch (Exception ex) {
               MessageBox.Show("Error sending email: " + ex.Message);
               Debug.WriteLine(ex.Message);
           }
       }

but with this code, I get this error

User's image

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,679 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,299 questions
{count} votes

4 answers

Sort by: Most helpful
  1. TP 76,846 Reputation points
    2023-01-23T23:56:37.7966667+00:00

    Hi,

    I suggest you take a different approach. When the end user fills out form, you send their email address, text, and maybe current version of your software they are running (if you want) to an api/webapp/function running in Azure. In your server code you could simply send email to yourself using SendGrid, or store the data in a Storage Queue and process it later, or whatever you like.

    The above can all be done with free tier, so no ongoing monthly cost.

    In this way, you don't need to imbed any app password in your exe, don't have to deal with any SMTP issues/possible outbound blocking of port by ISP, etc.

    I just used Azure and SendGrid above as an example--you can do same with other services that are free or very minimal monthly cost.

    If any of the above is unclear please let me know. It's not much code/time to accomplish it.

    If you found this useful please click Accept Answer.

    Thanks.

    -TP


  2. Bruce (SqlWork.com) 56,926 Reputation points
    2023-01-24T17:03:23.19+00:00

    the from address must match the authenticated user email address.

    note: also SmtpClient does not support OutLook.com (or Office) anymore as oauth authentication is required and SmptClient does not support oauth.


  3. Hui Liu-MSFT 40,666 Reputation points Microsoft Vendor
    2023-01-30T09:24:26.8166667+00:00

    Hi,@Eduardo Gomez.Welcome Microsoft Q&A.

    The clue in all of the above trace is the "SendAs" in "SendAsDeniedException". It means you authenticated using one email address but tried to send an email using another email address for the "FROM" address. (i.e. you are "sending as" another person).

    You could try to check whether the mailbox address and certified mailbox address of the FROM is the same account.


    If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Karen Payne MVP 35,196 Reputation points
    2023-01-30T11:57:41.0966667+00:00

    Your best option is to use MailKit, see the docs Using OAuth2 With Exchange (IMAP, POP3 or SMTP)

    0 comments No comments