Jack Jack - 1 year ago 54
Java Question

How to open each email in gmail?

Could you please help with follow?


  1. I need to open each email that I have in my Inbox.

  2. Get a content from it.






public void main() {
driver.get("https://mail.google.com");

// gmail login
driver.findElement(By.id("Email")).sendKeys("email");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();


List<WebElement> unreademeil = driver.findElements(By.xpath("//*[@class='zF']"));

// Mailer name for which i want to check do i have an email in my inbox
String MyMailer = "Команда Gmail";

// real logic starts here
for(int i=0;i<unreademeil.size();i++){
if(unreademeil.get(i).isDisplayed()==true){

if(unreademeil.get(i).getText().equals(MyMailer)){
System.out.println("Yes we have got mail form " + MyMailer);

break;
}else{
System.out.println("No mail form " + MyMailer);
}
}
}
//open a mail from the gmail inbox.
List<WebElement> a = driver.findElements(By.xpath("//*[@class='yW']/span"));
System.out.println(a.size());
for (int i = 0; i < a.size(); i++) {
System.out.println(a.get(i).getText());
if (a.get(i).getText().equals("Я")) //to click on a specific mail.
{
a.get(i).click();
System.out.println(driver.findElement(By.xpath("//*[@id=\":9v\"]/div[1]")).getText());
driver.navigate().back();
}
}


I've tried JavaMail API as well.

But it seems that I do something wrong in their code.

public static void bot() throws Exception {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "@gmail.com",
"password");

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

System.out.println("Total Message:" + folder.getMessageCount());
System.out.println("Unread Message:"
+ folder.getUnreadMessageCount());

Message[] messages = null;
boolean isMailFound = false;
Message mailFromGod= null;

//Search for mail from God
for (int i = 0; i < 5; i++) {
messages = folder.search(new SubjectTerm("t");
folder.getMessages());
//Wait for 10 seconds
if (messages.length == 0) {
Thread.sleep(10000);
}
}

for (Message mail : messages) {
if (!mail.isSet(Flags.Flag.SEEN)) {
mailFromGod = mail;
System.out.println("Message Count is: "
+ mailFromGod.getMessageNumber());
isMailFound = true;
}
}
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(
new InputStreamReader(mailFromGod
.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
System.out.println(buffer);

String registrationURL = buffer.toString().split("http://www./?")[0]
.split("href=")[1];
System.out.println(registrationURL);
}
}
}


and try this

package Bots;

import javax.mail.*;
import java.util.Properties;

public class checkemail {

public static void check(String host, String storeType, String user,
String password)
{
try {

//create properties field
Properties properties = new Properties();

properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", "995");
properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);

//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");

store.connect(host, user, password);

//create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);

// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);

for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
//System.out.println("Message" + message.getDescription().toString());

}

//close the store and folder objects
emailFolder.close(false);
store.close();

} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {

String host = "pop.gmail.com";// change accordingly
String mailStoreType = "pop3";
String username = "[email protected]";// change accordingly
String password = "test";// change accoredingly

check(host, mailStoreType, username, password);

}

}


Result:
...
Email Number 7
Subject: test
From: Google

Text: [email protected]



Email Number 8
Subject: test
From: Google
Text: [email protected]

but how to get normal text from the message

Answer Source
message.getContent().toString()

Above code works for plain text for multipart messages please include below piece of code in you for loop.

Multipart multipart = (Multipart) message.getContent();
for (int j = 0; j < multipart.getCount(); j++) {
BodyPart bodyPart = multipart.getBodyPart(j);
System.out.println("Body: "+bodyPart.getContent());
content= bodyPart.getContent().toString();
System.out.println(content);
}

Lets update if your issue resolves by this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download