1

I want to get help with how to send the console output via email on a click button function in my program.

The variable textBox2.Text contains the text that is being printed out to the console and I want this text to be send automatically on the (button1_Click_1) function.

I have found some solutions on somewhat similar questions but none of them seem to work, I hope I can find a solution here.

My code:

private void textBox2_TextChanged(object sender, EventArgs e)
{
    Console.WriteLine(textBox2);
}

private void button1_Click_1(object sender, EventArgs e)
{

 //Sending email function with the console output that is being printed from (textBox2.Text) should be here.

    Taskbar.Show();
    System.Windows.Forms.Application.Exit();
}
haldo
  • 14,512
  • 5
  • 46
  • 52
  • 2
    There is a lot questions and tutorials all over the internet how to send email with some text. Have a look here: http://csharp.net-informations.com/communications/csharp-smtp-mail.htm and after some tries if it still doesnt work, post your code here and we can help you more. Its not like that we will code entire thíing for you. :) Or if you already tried something, post it here for review. – Vojtěch Mráz Feb 02 '20 at 19:08

1 Answers1

0

Microsoft recommends that MailKit and MimeKit libraries be used for sending emails from C# applications.

Here is the working code snippet in C# for sending an email:

// File name:  SendMail.cs

using System;

using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

namespace SendMail {
    class Program
    {
        public static void Main (string[] args) {
            using (var client = new SmtpClient ()) {

                // Connect to the email service (Accept ssl certificate)
                client.ServerCertificateValidationCallback = (s,c,h,e) => true;
                client.Connect ("smtp.friends.com", 587, false);


                // Optional step: Send user id, password, if the server requires authentication
                client.Authenticate ("emailID", "emailPassword");


                // Construct the email message
                var message = new MimeMessage();
                message.From.Add (new MailboxAddress ("Sender's Name", "sender-email@example.com"));
                message.To.Add (new MailboxAddress ("Receiver's Name", "receiver-email@example.com"));
                message.Subject = "Automatic email from the application";
                message.Body = new TextPart ("plain") { Text = @"Hello Customer, Happy new year!"};


                // Send the email
                client.Send (message);


                // Close the connection with the email server
                client.Disconnect (true);
            }
        }
    }
}

More information:

https://github.com/jstedfast/MailKit

Gopinath
  • 4,066
  • 1
  • 14
  • 16