I am reading email from gmail.com. i have read some of the mail successfully but after a while i got this,
java.lang.ClassCastException:
javax.mail.internet.MimeMultipart cannot be cast to java.lang.String
at emailIngestion.EmailIngestion.check(EmailIngestion.java:66)
at emailIngestion.EmailIngestion.main(EmailIngestion.java:106).
Actually my requirement is to store content into a variable and then store it in arraylist and then again write it to a file... i have implemented it using this code so if any better ideas are there please share me.
public class EmailIngestion {
static ArrayList<EmailModel> contentList=new ArrayList<EmailModel>();
static ArrayList<EmailModel> metaDataList= new ArrayList<EmailModel>();
public static void check(String host, String storeType, String user,
String password) throws IOException
{
FileWriter fw= new FileWriter("C:\\Users\\Admin\\Desktop\\murtaza_metadata.csv",true);
FileWriter fw1= new FileWriter("C:\\Users\\Admin\\Desktop\\murtaza_content.txt",true);
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);
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
int emailNumber;
String mailContent = null,from = null,to = null,mailContentType = null,subject = null;
Date recievedDate= new Date();
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
System.out.println("Subject is"+message.getSubject());
subject = message.getSubject();
from = message.getFrom()[0].toString();
mailContentType=message.getContentType();
recievedDate=message.getSentDate();
to=InternetAddress.toString(message.getRecipients(Message.RecipientType.TO));
emailNumber=message.getMessageNumber();
metaDataList.add(new EmailModel(from, to, subject, mailContentType, recievedDate,emailNumber));
fw.write(emailNumber+"\001"+from+"\001"+subject+"\001"+recievedDate+mailContentType+"\001"+" \n");
fw.flush();
if(message.isMimeType("multipart/*")){
Multipart multipart = (Multipart) message.getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
mailContent=(String) bodyPart.getContent();
System.out.println(mailContent);
}
}
else{
mailContent=(String) message.getContent();
System.out.println(message.getContent());
}
contentList.add(new EmailModel(mailContent, emailNumber));
fw1.write(emailNumber+","+mailContent+"\n");
}
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) throws IOException {
String host = "pop.gmail.com";// change accordingly
String mailStoreType = "pop3";
String username = "abcd#gmail.com";// change accordingly
String password = "******";// change accordingly
check(host, mailStoreType, username, password);
}
}
Thanks in advance
If the services are fluctuating then its definitely not stable. Which version of 1.3 you are using? As you are using one node make sure that you have master and worker services are installed and running. This you can see from your cluster manager. The info from logs is not informative enough to nail down the issue.
Related
I've been constructing a software that will send an email verification code, however when ever I try to run it I get an error on this line ' newSession = Session.getInstance(properties,null);'
Does anyone knows why this is happening, and would also be willing to give me some pointers in how I could fix this?
public void emailVerification()
{
int min = 0;// sets a minimum number for range
int max = 9;//sets maximum number for range
int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);// generates a random number
int random_int2 = (int)Math.floor(Math.random()*(max-min+1)+min);// generates another random number
int random_int3 = (int)Math.floor(Math.random()*(max-min+1)+min);// generates another random number
int random_int4 = (int)Math.floor(Math.random()*(max-min+1)+min);// generates the final random number
String randomDigit1 = random_int+"";
String randomDigit2 = random_int2+"";
String randomDigit3 = random_int3+"";
String randomDigit4 = random_int4+"";
allDigits = randomDigit1+randomDigit2+randomDigit3+randomDigit4;
Session newSession = null;
MimeMessage mimeMessage = null;
Properties properties = new Properties();
properties.put("mail.smtp.auth","true");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.port","547");
String emailHost = ("smtp.gmail.com");
String myAccountEmail = ("**********#*****.com");
String myAccountPassword = ("*******");
newSession = Session.getInstance(properties,null);
String[] emailRecipients ={signUpAccount};
String emailSubject = "Verification Code";
String emailBody = allDigits;
mimeMessage = new MimeMessage(newSession);
for(int i =0; i<emailRecipients.length;i++)
{
try
{
mimeMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(emailRecipients[i]));
}
catch (javax.mail.MessagingException me)
{
me.printStackTrace();
}
}
try
{
mimeMessage.setSubject(emailSubject);
}
catch (javax.mail.MessagingException me)
{
me.printStackTrace();
}
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart bodyPart = new MimeBodyPart();
try
{
multipart.addBodyPart(bodyPart);
}
catch (javax.mail.MessagingException me)
{
me.printStackTrace();
}
try
{
mimeMessage.setContent(multipart);
}
catch (javax.mail.MessagingException me)
{
me.printStackTrace();
}
try
{
Transport transport = newSession.getTransport("smtp");
try
{
transport.connect(emailHost, myAccountEmail, myAccountPassword);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
JOptionPane.showInputDialog("The verification Email has been sent");
}
catch (javax.mail.MessagingException me)
{
me.printStackTrace();
}
}
catch (javax.mail.NoSuchProviderException nspe)
{
nspe.printStackTrace();
}
}
My company mail domain is Gmail , and I need to search a mail using subject line. I have written below code but Gmail is not connecting with correct user id and password.
public static void getMail() {
Scanner sc = new Scanner(System.in);
final String m10 = "abc#abc.com";
final String n10 = "abcd";
String host = "absmtp.abc.com";
try
{
Properties pro1 = new Properties();
pro1.put("mail.smtp.host", host);
pro1.put("mail.smtp.socketFactory.port", "465");
MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
pro1.put("mail.imaps.ssl.socketFactory", socketFactory);
// pro1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// pro1.put("mail.smtp.auth", "true");
pro1.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(pro1);
Store store = session.getStore("imaps");
store.connect(host, m10, n10);
Folder folderbox = store.getFolder("INBOX");
folderbox.open(Folder.READ_ONLY);
SearchTerm search = new SearchTerm(){
#Override
public boolean match(Message message) {
try
{
if(message.getSubject().contains("") )
{
return true;
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return false;
}
};
Message[] found = folderbox.search(search);
int length = found.length;
for(int i = 0;i<found.length;i++)
{
Message mess1 = found[i];
System.out.println("->Message Number > "+i);
System.out.println("->Message Subject >"+mess1.getSubject());
}
folderbox.close(true);
store.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
is not connecting and giving error :
Couldn't connect to host, port: abc#abc.com, 993; timeout -1.
I am implementing a Bulk mail application, in this applica This link.
I am able to connect to the server and and send the emails to the destination addresses, but I want to identify the undelivered mails.
By using below program I am able to get the email subjects. But based on the subject it will be difficult to identify the exact undelivered mails.
public static void main(String[] args) {
Properties props = System.getProperties();
props.setProperty("mail.host", host);
props.setProperty("mail.user", user);
props.setProperty("mail.from", from);
//props.setProperty("mail.debug", "true");
//props.setProperty("mail.domain", domain);
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore(protocol);
Session session1 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
System.out.println((store.isConnected())?"Already Connected":"Not Already Connected");
store.connect(host, port, user, password);
Folder inbox = store.getFolder("INBOX");
System.out.println("folder>>>" + inbox.getFullName() + "<<<");
System.out.println("folder URLName>>>" + inbox.getURLName() + "<<<");
System.out.println((inbox.exists()?"folder exists":"folder does not exist"));
int folderType = inbox.getType();
System.out.println("folder type>>>" + folderType + "<<<");
inbox.open(Folder.READ_WRITE);
System.out.println("Message Count:" + inbox.getMessageCount());
Message[] m = inbox.getMessages();
for (int x = 0; x < m.length; x++) {
System.out.println(m[x].getSubject());
}
inbox.close(false);
store.close();
}catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
How can I get the undelivered mails(Bounced).
I am using Hmailserver as my mail server.
You can use this below code
MimeMessage payload = (MimeMessage) message.getPayload();
Multipart mp = (Multipart) payload.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bodyPart = mp.getBodyPart(i);
StringWriter writer = new StringWriter();
IOUtils.copy(bodyPart.getInputStream(), writer);
System.out.println("Content inputstream: " + writer.toString());
}
I'm trying to get all mails of my gmail but I can only get 250. According to https://javaee.github.io/javamail/FAQ#gmailsettings, I had already set the number of mail limit to 1000 but it still only fetch 250 mails. Here's my code
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
public class Getreply {
public static void check() {
String host = "pop.gmail.com";
String mailStoreType = "pop3";
String username = "";
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, username, 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];
Object body = message.getContent();
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: " + body );
}
//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();
}
}
}
Does anyone know how can I get all?
Check your POP settings in Gmail or switch to IMAP.
I have just test it if you have no need to use pop you should try imap
your chech method will be like this below. Idid test it i can my mails without restriction. But also if you configure your gmail account for a specific number you will receive that number.
public static void check() {
String host = "imap.gmail.com";
String username = "user";
String password = "passwd";
try {
//create properties field
Properties properties = new Properties();
properties.setProperty("mail.imap.ssl.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("imap");
store.connect(host, username, 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];
Object body = message.getContent();
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: " + body );
}
//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();
}
}
I am trying to download attachments from Hotmail messages and getting the following exception: com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 2 most recent characters were: "JV". for every message with an attachment. I tried setting "mail.imaps.partialfetch" to "false" but it did not work, here is the code i'm using:
public class imapHotmail {
private static Object m;
public static void main(String[] args) {
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
//System.setProperty("mail.mime.decodetext.strict", "false");
//System.setProperty("mail.mime.base64.ignoreerrors", "true");
System.setProperty("mail.imaps.partialfetch", "false");
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imap.ssl.enable", "true");
props.setProperty("mail.imaps.partialfetch", "false");
try {
Session session = Session.getInstance(props, null);
// session.setDebug(true);
Store store = session.getStore("imap");
System.out.println("connecting....");
store.connect("imap-mail.outlook.com", "soemone#hotmail.com", "pass");
System.out.println("connected");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
Message[] msgs = inbox.getMessages();
int x = 1;
for (Message msg : msgs) {
System.out.println(x);
System.out.println(msg.getSubject());
Object content = msg.getContent();
if (content instanceof Multipart) {
Multipart multipart = (Multipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(i);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
System.out.println("saving");
//part.getContent(); this only returns the parts headers and 2 chars from the encoded bodypart
String name = part.getFileName();
File f = new File("C:\\Users\\user\\Desktop\\" + name);
part.saveFile("C:\\Users\\user\\Desktop\\" + name);
System.out.println("saved");
}
}
}
x++;
}
inbox.close(true);
store.close();
} catch (Exception mex) {
mex.printStackTrace();
}
}
here is the output i get if i call part.writeTo(System.out):
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf2.pdf"
JV