· 9 years ago · Jul 26, 2017, 03:06 PM
1public void main() {
2 driver.get("https://mail.google.com");
3
4 // gmail login
5 driver.findElement(By.id("Email")).sendKeys("email");
6 driver.findElement(By.id("next")).click();
7 driver.findElement(By.id("Passwd")).sendKeys("password");
8 driver.findElement(By.id("signIn")).click();
9
10
11 List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
12
13 // Mailer name for which i want to check do i have an email in my inbox
14 String MyMailer = "Команда Gmail";
15
16 // real logic starts here
17 for(int i=0;i<unreademeil.size();i++){
18 if(unreademeil.get(i).isDisplayed()==true){
19
20 if(unreademeil.get(i).getText().equals(MyMailer)){
21 System.out.println("Yes we have got mail form " + MyMailer);
22
23 break;
24 }else{
25 System.out.println("No mail form " + MyMailer);
26 }
27 }
28 }
29 //open a mail from the gmail inbox.
30 List<WebElement> a = driver.findElements(By.xpath("//*[@class='yW']/span"));
31 System.out.println(a.size());
32 for (int i = 0; i < a.size(); i++) {
33 System.out.println(a.get(i).getText());
34 if (a.get(i).getText().equals("Я")) //to click on a specific mail.
35 {
36 a.get(i).click();
37 System.out.println(driver.findElement(By.xpath("//*[@id=":9v"]/div[1]")).getText());
38 driver.navigate().back();
39 }
40 }
41
42public static void bot() throws Exception {
43 Properties props = System.getProperties();
44 props.setProperty("mail.store.protocol", "imaps");
45
46 Session session = Session.getDefaultInstance(props, null);
47 Store store = session.getStore("imaps");
48 store.connect("imap.gmail.com", "@gmail.com",
49 "password");
50
51 Folder folder = store.getFolder("INBOX");
52 folder.open(Folder.READ_WRITE);
53
54 System.out.println("Total Message:" + folder.getMessageCount());
55 System.out.println("Unread Message:"
56 + folder.getUnreadMessageCount());
57
58 Message[] messages = null;
59 boolean isMailFound = false;
60 Message mailFromGod= null;
61
62 //Search for mail from God
63 for (int i = 0; i < 5; i++) {
64 messages = folder.search(new SubjectTerm("t");
65 folder.getMessages());
66 //Wait for 10 seconds
67 if (messages.length == 0) {
68 Thread.sleep(10000);
69 }
70 }
71
72 for (Message mail : messages) {
73 if (!mail.isSet(Flags.Flag.SEEN)) {
74 mailFromGod = mail;
75 System.out.println("Message Count is: "
76 + mailFromGod.getMessageNumber());
77 isMailFound = true;
78 }
79 }
80 String line;
81 StringBuffer buffer = new StringBuffer();
82 BufferedReader reader = new BufferedReader(
83 new InputStreamReader(mailFromGod
84 .getInputStream()));
85 while ((line = reader.readLine()) != null) {
86 buffer.append(line);
87 System.out.println(buffer);
88
89 String registrationURL = buffer.toString().split("http://www./?")[0]
90 .split("href=")[1];
91 System.out.println(registrationURL);
92 }
93 }
94}
95
96package Bots;
97
98import javax.mail.*;
99import java.util.Properties;
100
101public class checkemail {
102
103 public static void check(String host, String storeType, String user,
104 String password)
105 {
106 try {
107
108 //create properties field
109 Properties properties = new Properties();
110
111 properties.put("mail.pop3.host", host);
112 properties.put("mail.pop3.port", "995");
113 properties.put("mail.pop3.starttls.enable", "true");
114 Session emailSession = Session.getDefaultInstance(properties);
115
116 //create the POP3 store object and connect with the pop server
117 Store store = emailSession.getStore("pop3s");
118
119 store.connect(host, user, password);
120
121 //create the folder object and open it
122 Folder emailFolder = store.getFolder("INBOX");
123 emailFolder.open(Folder.READ_ONLY);
124
125 // retrieve the messages from the folder in an array and print it
126 Message[] messages = emailFolder.getMessages();
127 System.out.println("messages.length---" + messages.length);
128
129 for (int i = 0, n = messages.length; i < n; i++) {
130 Message message = messages[i];
131 System.out.println("---------------------------------");
132 System.out.println("Email Number " + (i + 1));
133 System.out.println("Subject: " + message.getSubject());
134 System.out.println("From: " + message.getFrom()[0]);
135 System.out.println("Text: " + message.getContent().toString());
136 //System.out.println("Message" + message.getDescription().toString());
137
138 }
139
140 //close the store and folder objects
141 emailFolder.close(false);
142 store.close();
143
144 } catch (NoSuchProviderException e) {
145 e.printStackTrace();
146 } catch (MessagingException e) {
147 e.printStackTrace();
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 }
152
153 public static void main(String[] args) {
154
155 String host = "pop.gmail.com";// change accordingly
156 String mailStoreType = "pop3";
157 String username = "test@gmail.com";// change accordingly
158 String password = "test";// change accoredingly
159
160 check(host, mailStoreType, username, password);
161
162 }
163
164}
165
166message.getContent().toString()
167
168Multipart multipart = (Multipart) message.getContent();
169for (int j = 0; j < multipart.getCount(); j++) {
170BodyPart bodyPart = multipart.getBodyPart(j);
171System.out.println("Body: "+bodyPart.getContent());
172content= bodyPart.getContent().toString();
173System.out.println(content);
174}
175
176import org.openqa.selenium.Alert;
177import org.openqa.selenium.By;
178import org.openqa.selenium.WebDriver;
179import org.openqa.selenium.WebElement;
180import org.openqa.selenium.chrome.ChromeDriver;
181import org.testng.annotations.AfterMethod;
182import org.testng.annotations.BeforeMethod;
183import org.testng.annotations.Test;
184import java.util.ArrayList;
185import java.util.List;
186import java.util.concurrent.TimeUnit;
187
188public class email {
189
190private WebDriver driver;
191
192@BeforeMethod
193public void beforeMethod() {
194
195 String exePath = "chromedriver_win32\chromedriver.exe";
196 System.setProperty("webdriver.chrome.driver", exePath);
197 driver = new ChromeDriver();
198
199 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
200 driver.manage().window().maximize();
201 driver.get("https://google.com/");
202
203}
204
205@AfterMethod
206public void afterMethod() {
207 driver.quit();
208}
209
210@Test
211public void main() throws InterruptedException {
212
213 String email = "email";
214 String password = "password";
215
216 driver.get("https://mail.google.com");
217 driver.findElement(By.id("Email")).sendKeys(email);
218 driver.findElement(By.id("next")).click();
219 driver.findElement(By.id("Passwd")).sendKeys(password);
220 driver.findElement(By.id("signIn")).click();
221
222 // now talking un-read email form inbox into a list
223 List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
224// Mailer name for which i want to check do i have an email in my inbox
225
226 String MyMailer = "FROMTESTEMAIL";
227 String bodyemail = "";
228 int i = 0;
229
230 for (i = 0; i < unreademeil.size(); i++) {
231 if (unreademeil.get(i).isDisplayed() == true) {
232
233 unreademeil.get(i).click();
234 System.out.println(bodyemail);
235
236 driver.findElement(By.xpath("//a[contains(text(),'Click here')]")).click();
237
238 Thread.sleep(5000);
239 System.out.println("Your current page is: " + driver.getTitle());
240
241 ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
242 driver.switchTo().window(tabs2.get(0));
243 driver.close();
244 driver.switchTo().window(tabs2.get(1));
245
246 System.out.println("Your current page is: " + driver.getTitle());
247 driver.findElement(By.xpath("/html/body/div/div[5]/div[2]/div[1]/a")).click();
248 System.out.println("It is started");
249 Thread.sleep(3000);
250
251 // do something after clicking on the required link
252 // ...
253
254 try {
255 Alert confirmationAlert = driver.switchTo().alert();
256 String alertText = confirmationAlert.getText();
257 System.out.println("Alert text is " + alertText);
258 confirmationAlert.accept();
259 } catch (Exception e) {
260 System.out.println("The alerts haven't been found");
261 e.printStackTrace();
262 }
263
264 driver.get("https://mail.google.com");
265 Thread.sleep(5000);
266 i--;
267 unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));
268 }
269 }
270 }
271 }