0

I am creating an Email Sending Software. So far i am able to open chrome and fill the details of the email (subject,body,attachments and all) but i wanted to know is there any way i can click Send button of G-Mail programmatically too?

Code for filling details in chrome

Process ps = Process.Start("chrome.exe", "https://mail.google.com/mail/u/0/?view=cm&fs=1&tf=1&to=a@gmail.com&bcc=b@gmail.com);

Note I haven't used SmtpClient to send email because it was not fulfilling my customized requirement (sorry cannot disclose the reason).


Cœur
  • 37,241
  • 25
  • 195
  • 267
Developer Nation
  • 374
  • 3
  • 4
  • 20
  • 1
    How did you fill in the email details? Would you provide some code so we can see how and what you have done so far. – Gnqz Nov 04 '15 at 08:50
  • Have you looked at Selenium? It might do exactly what you need. Take a looke at the code in this example http://scraping.pro/example-of-scraping-with-selenium-webdriver-in-csharp/ – Jakob Olsen Nov 04 '15 at 08:52
  • @Gnqz please see the updated question. – Developer Nation Nov 04 '15 at 08:54
  • Not sure if you could send a message using Process.Start. Have you though about using Office Interop Outlook? http://stackoverflow.com/questions/11223462/how-to-send-a-mail-using-microsoft-office-interop-outlook-mailitem-by-specifying – Gnqz Nov 04 '15 at 08:59

3 Answers3

2

The way you want this, is not possible. Process.Start opens a new Process. In this case it opens Google Chrome, with the URL you sent with it. This URL contains a querystring containing the subject and the body of the email. This is possible, because Google reads this on the server-side, and puts it in the right fields. There is no way to enter the send button though. This is a POST action, which can not be triggered by a URL.

C# form, does have a WebBrowser class. From here you can access buttons and click them, but I don't think Google will allow this, and most likely send you a captcha. (That is, if you manage to login in the first place.)

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • first i was using "WebBrowser" but in PC's with Windows XP they required number of setup's to be installed first which is not feasible. – Developer Nation Nov 04 '15 at 09:10
  • Used "process" because i am not only using it for chrome but i am integrating it with firefox, opera and internet explorer too and not only G-Mail but yahoo,rediff etc. – Developer Nation Nov 04 '15 at 09:12
  • I see. Unfortunately this only opens the browser. After it has opened with a specific URL, you have no control over it. – Ivar Nov 04 '15 at 09:14
  • yes that is why i am asking is there any way that through the process i cant send a command or by changing the code with "HttpRequest" is it possible to achieve it? – Developer Nation Nov 04 '15 at 09:16
  • Since you can't give any commands to the browsers, the only option I see is to use an class similar to the WebBrowser class. (I'm pretty sure there are other (3rd party) libraries that support this). This is the only way you can actually access the buttons. The only problem is that a lot of websites (certainly Google) are protected against this, and most likely will require a captcha before you can do anything. So I'm afraid that what you want is not possible. – Ivar Nov 04 '15 at 09:20
  • basically i switched the code from "SmtpClient" because recently "google" made some changes which started to pop some new error like this one "the remote certificate is invalid according to the validation procedure" and i thought to avoid this i should apply the above explained steps. what do you think about it? – Developer Nation Nov 04 '15 at 09:30
  • I think that the SmtpClient is still your best bet. I once had a similar notification. but I managed to suppress it. (For the WebBrowser in my case, but the message was similar.) Maybe you can try that? http://stackoverflow.com/questions/5042982/where-to-place-remotecertificatevalidationcallback – Ivar Nov 04 '15 at 09:35
  • i tried the accepted solution but the problem was solved only for some users not all that is why i thought it would be good to switch to the above code. – Developer Nation Nov 04 '15 at 09:37
1

I can't help with the simulated button press you're looking for, but I can suggest that you rather try to use the Gmail API to do what you're trying to do: https://developers.google.com/gmail/api/?hl=en

This would be a more reliable and stable way to send gmail programmatically, and you still don't have to use the smtp object directly.

  • you might be right but not only for chrome but i am integrating it with firefox, opera and internet explorer too and not only G-Mail but yahoo,rediff etc. – Developer Nation Nov 04 '15 at 09:11
0

You should probably use Selenium (WebDriver). It allows you to control browser from c# code, navigates pages, traversing DOM etc..

Andrey Ischencko
  • 800
  • 1
  • 6
  • 18
  • you might be right but not only for chrome but i am integrating it with firefox, opera and internet explorer too and not only G-Mail but yahoo,rediff etc – Developer Nation Nov 04 '15 at 11:33