Reputation: 117
I have two activities, one for recieving emails using Imap the second is to send email using SMTP. The button for send email activity is inside the Recieve Email activity, So the Recieve Email Activity has to run before Send Email ACitivty..
My problem is I keep getting an Exception saying
Could not connect to SMTP host: localhost, port: 25
at Transport.send(new_message)
However, if I run the same activity without calling the recieve email activity first, then it works without any problem. Can someone help me as to why this happens?
Here's the Code for RecieveEmail Async task (the full Activity code is too long)
try{
Properties props = new Properties();
props.setProperty( "mail.imaps.socketFactory.class", "com.X509TrustAll.DummySSLSocketFactory" );
Log.v("EmailList", "Stting properties");
// Get the default Session object.
session = Session.getDefaultInstance(props, null);
Log.v("EmailList", "Geting Default Instance");
// Get a Store object that implements the specified protocol.
store = session.getStore("imaps");
Log.v("EmailList", "Getting Sesion");
//Connect to the current host using the specified username and password.
Log.v("EmailList", "Connecting...");
store.connect(host,port, user, password);
Log.v("EmailList", "Connected");
//Create a Folder object corresponding to the given name.
Folder[] folders = store.getDefaultFolder().list("*");
Log.v("EmailList", "Got Folder List");
folder = folders[5];
// Open the Folder.
Log.v("EmailList", "Opening Folder");
folder.open(Folder.READ_ONLY);
Log.v("EmailList", "Getting Messages");
messages = folder.getMessages();
Log.v("EmailList", "Got Messages");
} catch (Exception e){
Log.v("EmailList", "Exception");
}
}
for (int i = lastMessageNumber -1; i >= interateUpto; i--) {
Log.v("EmailRecieve", "Email no. " + Integer.toString(i));
javax.mail.Address[] froms = messages[i].getFrom();
String emailAdress = froms == null ? null : ((InternetAddress) froms[0]).getAddress();
FromAsync.add(emailAdress);
SubjectAsync.add(messages[i].getSubject());
SentDateAsync.add(messages[i].getSentDate().toString());
Part part = messages[i];
checkAttachments(part);
if (messages[i].isSet(Flag.SEEN)){
SeenAsync.add("true");
}else{
SeenAsync.add("false");
}
}
lastMessageNumber = lastMessageNumber - 19; //new last number is stored
folder.close(false);
store.close();
} catch (Exception e){
Log.v("EmailList", "Exception");
}
Log.v("EmailList", "Retrieve Email Finished");
return null;
}
And Here's the code for Send Email (again the full code is too long so I'm only posting whats relevant, i.e UI etc. are not posted
@Override
protected Void doInBackground(Void... arg0) {
//Recipient's email ID needs to be mentioned.
String to = To.getText().toString();
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// The Host
String smtphost = "www.isleworthsyon.hounslow.sch.uk";
// Get system properties
Properties smtpproperties = System.getProperties();
// Setup mail server
smtpproperties.setProperty("mail.smtp.host", smtphost);
// Get the default Session object.
Session smtpsession = Session.getDefaultInstance(smtpproperties);
try{
MimeMessage new_message = new MimeMessage(smtpsession);
// Set From:
new_message.setFrom(new InternetAddress(from));
// Set To:
new_message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject:
new_message.setSubject(Subject.getText().toString());
if (hasAttachment == true){
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(Body.getText().toString());
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Iterating over all Attachments
for (int i=0; i < attachmentFiles.size(); i++){
if (attachmentFiles.get(i).equals("null") != true) {
messageBodyPart = new MimeBodyPart();
String filename = attachmentFiles.get(i);
FileDataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
}
new_message.setContent(multipart);
}else{
// Set Body if not attaching anything
new_message.setText(Body.getText().toString());
}
// Send message
Transport.send(new_message);
sendingSuccesfull = true;
}catch (MessagingException mex) {
sendingSuccesfull = false;
Log.v("Email Compose", "Message Sending Failed, Details: " + mex.getMessage());
}
return null;
}
Upvotes: 0
Views: 422
Reputation: 29971
Try cleaning up these common mistakes and see if that solves the problem.
Upvotes: 1