1

I have managed to create simple app that will send e-mail with specific text, but I am wondering that is this possible to send the same e-mail, but with content of text being copied into clipboard?

In my oMail.TextBody I would like to paste content of clipboard and e-mail it.

    static void Main(string[] server)
    {
        SmtpMail oMail = new SmtpMail("TryIt");
        EASendMail.SmtpClient oSmtp = new EASendMail.SmtpClient();

        // Set sender email address
        oMail.From = "myEmail";

        // Set recipient email address
        oMail.To = "myEmail";

        // Set email subject
        oMail.Subject = "test email from c# project";

        // Set email body
        oMail.TextBody = "Clipboard content pasted here..."
     }

Is there any way to do it? Additionally I am using EASendMail namespace.

Arcadio R
  • 21
  • 3

1 Answers1

1

In console app, clipboard is accessible in certain thread states, specifically STA.

Take a look at this SO question for explanation.

So, write a static method like this:

static string GetClipboardText()
{
    string result = string.Empty;
    Thread staThread = new Thread(x =>
    {
        try
        {
            result = Clipboard.GetText();
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
    });
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
    return result;
}

and use it in your main method

oMail.TextBody = GetClipboardText();
Community
  • 1
  • 1
Nino
  • 6,931
  • 2
  • 27
  • 42
  • Thanks for your answer. However what I have done was leaving code as it was with no changes, but I have just added `[STAThread]`above `static void Main(string[] server)`. Works for me. Thank you – Arcadio R Jan 23 '17 at 12:55