Can't send email after checking for new ones..?
Hi all,
I have a weird problem. Well, weird as in I can't figure it out. :-)
I have 2 methods. One gets email (checks for new and stores the new), the other sends email.
Each one used by themselves ONLY work fine. The problem comes when I call sendEmail right after getEmails...
Code is as follows:
// getEmails()
privateint getEmails(){
String host ="test.de";
String username ="test@test.com";
String password ="test";
int count = 0;// number of Emails read in
// Create empty properties
Properties props =new Properties();
// Get session
Session session = Session.getDefaultInstance(props,null);
try{
// Get the store
Store store = session.getStore("imap");
store.connect(host, username, password);
// Get folder
Folder folder = store.getFolder("INBOX");
if (folder.exists()){
folder.open(Folder.READ_ONLY);
//JOptionPane.showMessageDialog(frame, "Folder \"" + folder.getName() + "\" exists! Yay!", "WOOHOO", JOptionPane.PLAIN_MESSAGE);
}
else{
JOptionPane.showMessageDialog(frame,"Folder \"" + folder.getName() +"\" does not exist","ERROR", JOptionPane.ERROR_MESSAGE);
return count;
}
// Get directory
Message message[];// to store the new messages
int numOfMsgs = folder.getMessageCount();
message = folder.getMessages();
int unreadCount = 0;
for (int i=message.length - 1; i > -1; i--){
if (!message[i].isSet(Flags.Flag.SEEN)){
String[] messageid = message[i].getHeader("Message-ID");
FileWriter newEmailsWriter =new FileWriter(newEmails,true);
System.out.println(messageid[0]);
newEmailsWriter.write("MessageID: " + messageid[0] +"\n");
newEmailsWriter.close();
if (message[i].getContentType().startsWith("text/plain")){
unreadCount = 0;
count++;
extractPlainMessage(message[i]);
}
else{
unreadCount = 0;
count++;
extractMessage(message[i]);
}
}
else{
unreadCount++;
}
// if no new messages have been read in 10/11 messages
// quit
if (unreadCount > 3){
break;
}
}// for
System.out.println("\n\n" + count +" new messages");
if (count == 0){
return count;
}
// Close connection
folder.close(true);
store.close();
}
catch(Exception e){
e.printStackTrace();
}
return count;
}
//sendEmails
private String sendEmails(int type, Vector <EmailInfo> adresse){
System.out.println(adresse.size());
String status ="";// return string value
System.out.println("Sending emails! Shiiiiiiiiit!");
String host ="test.de";
String from ="noreply@test.de";
String login ="test@test.de";
String pwd ="test";
java.util.Date todaysDate =new java.util.Date();// for the sent date
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Get session
Session session = Session.getDefaultInstance(props,null);
for (int i = 0; i < adresse.size(); i++){
// Define message
MimeMessage message =new MimeMessage(session);
try{
if (type == EMAIL_MESSAGE_CONFIRMATION){
// Set the from address
message.setFrom(new InternetAddress(from,"Test1"));
// Set the subject
message.setSubject("Confirmation");
message.setSentDate(todaysDate);
File confirmEmailFile =new File("Data/Mail/EmailText/Email_Confirmation.txt");
String emailText ="";
String emailFileLine ="";
if (confirmEmailFile.exists()){
try{
BufferedReader confirmEmailFileReader =new BufferedReader(new InputStreamReader(new FileInputStream("Data/Mail/EmailText/Email_Confirmation.txt")));
while ((emailFileLine = confirmEmailFileReader.readLine()) !=null){
emailText += emailFileLine;
emailText +="\n";
}
}
catch (Exception e){
e.printStackTrace();
status ="ERROR";
return status;
}
}
else{
status ="ERROR";
return status;
}
// Set the content
message.setText(emailText);
}// if
elseif (type == EMAIL_MESSAGE_INVOICE){
// Set the from address
message.setFrom(new InternetAddress(from,"test"));
// Set the subject
message.setSubject("Invoice");
message.setSentDate(todaysDate);
File invoiceEmailFile =new File("Data/Mail/EmailText/Email_Invoice.txt");
String emailText ="";
String emailFileLine ="";
if (invoiceEmailFile.exists()){
try{
BufferedReader invoiceEmailFileReader =new BufferedReader(new InputStreamReader(new FileInputStream("Data/Mail/EmailText/Email_Invoice.txt")));
while ((emailFileLine = invoiceEmailFileReader.readLine()) !=null){
emailText += emailFileLine;
emailText +="\n";
}
}
catch (Exception e){
e.printStackTrace();
status ="ERROR";
return status;
}
}
else{
status ="ERROR";
return status;
}
// Create the message part
BodyPart messageBodyPart =new MimeBodyPart();
// Fill the message
messageBodyPart.setText(emailText);
Multipart multipart =new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
// add invoice pdf attachment
File invoiceFile =new File("Data/Invoices/Waiting/R" + adresse.get(i).getOrderID().substring(1) +".pdf");
if (!invoiceFile.exists()){
JOptionPane.showMessageDialog(frame,"Invoice has not been created for this order. Please create\none and then try to send again.","File Not Found", JOptionPane.INFORMATION_MESSAGE);
return"ERROR";
}
BodyPart attachmentPart =new MimeBodyPart();
DataSource source =new FileDataSource(invoiceFile);
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName("Rechnung.pdf");
multipart.addBodyPart(attachmentPart);
// Put parts in message
message.setContent(multipart);
}// if
}// try
catch (Exception er){
er.printStackTrace();
status ="ERROR";
return status;
}
// add all adresse / recipients to message
// if order contains no email address
// should never be null or extraction failed, but just in case
if (adresse.get(i).getEmailAddress() ==null){
continue;
}
try{
String to = adresse.get(i).getEmailAddress();
// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, login, pwd);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (Exception error){
error.printStackTrace();
status ="ERROR";
return status;
}
}// for
status ="SUCCESS";
return status;
}
Does anyone have any idea why calling getEmails(), interferes with sendEmails if it is called directly after? In fact, if during the program cycle, I call getEmails(), sendEmails() will just not work. The error I get is:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1141)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:536)
Any ideas?
Thanks in advance for taking the time.
...DJVege...

