I'm working on an mailing app. The app can receive multiple recipient addresses. Once the "Send" button is pressed, a single message is sent containing multiple recipient addresses. Of course, by having this kind of system, there would be an SendFailedException if at least one of the addresses is invalid. Naturally, I would catch this and exception and try to work around with it. Here's my code so far:
catch (SendFailedException e)
{ try
{
System.out.println(e.getValidUnsentAddresses()!=null);
if (e.getValidUnsentAddresses()!=null) //with valid unsent addresses
{
String invalid = new String();
String valid = new String();
for (int i=0; i<e.getInvalidAddresses().length; i++)
{
invalid = invalid + e.getInvalidAddresses()[i].toString();
if (i+1<e.getInvalidAddresses().length)
{
invalid = invalid + ", ";
}
}
for (int i=0; i<e.getValidUnsentAddresses().length; i++)
{
valid = valid + e.getValidUnsentAddresses()[i].toString();
if (i+1<e.getValidUnsentAddresses().length)
{
valid = valid + ", ";
}
}
int choice = JOptionPane.showOptionDialog(null, invalid + " are invalid addresses. Send mail to valid addresses?", "Information", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, "No");
if (choice==0)
{
transport.sendMessage(message, e.getValidUnsentAddresses());
JOptionPane.showMessageDialog(null, "Sent message to " + valid + ".", "Information", JOptionPane.INFORMATION_MESSAGE);
}
}
else //no valid unsent addresses
{
JOptionPane.showMessageDialog(null, "All email addresses are invalid.", "Information", JOptionPane.ERROR_MESSAGE);
}
}
catch(MessagingException e2)
{ e2.printStackTrace(); }
}
Loop to add recipient addresses to message:
for (int i=0; i<listModel.getSize(); i++) //listModel is of DefaultListModel Class
{
message.addRecipient(Message.RecipientType.TO, new InternetAddress((String)listModel.getElementAt(i)));
}
Then at the end:
properties = System.getProperties();
session = Session.getDefaultInstance(properties);
transport = session.getTransport("smtp");
transport.connect(host, textEmail.getText(), new String(pwdPassword.getPassword()));
transport.sendMessage(message, message.getAllRecipients()); // <----this part throws the SendFailedException
transport.close();
Now onto the problem. You could see that there's an if-else statement immediately inside the catch block. I wanted to check whether there were any valid unsent addresses in the recipients. This way I can know if there are only invalid addresses in the recipients (as there are no valid sent addresses since the message fails to send because of the exception).
I've run some tests on the system. It works fine if all addresses are valid, however, when a single invalid address is included, a minor problem appears. If I were to put only invalid addresses in the recipients, it is supposed to go to the "else" part, but instead it always ends up in the "if" part of the statement.
While it is only a minor problem (it's only about prompts), I would still like to know why my condition doesn't work. I might be missing something about the SendFailedException
Try putting all invalid addresses and it should work
If is executed everytime because even if one valid address is there
e.getValidUnsentAddresses();
will not return null
Related
I am using the code from this tutorial with minimal modifications. Here is my code: `
public static void receiveEmail(String hst, String stype, String user, String password) {
try {
Properties props = new Properties();
props.put("mail.store.protocol", "pop3");
props.put("mail.pop3s.host", hst);
props.put("mail.pop3s.port", "995");
props.put("mail.pop3.starttls.enable", "true");
Session sess = Session.getDefaultInstance(props);
Store st = sess.getStore("pop3s");
st.connect(hst, user, password);
Folder emailFolder = st.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
Message message = messages[229];
System.out.println("Welcome To Email");
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
String body = "body";
Multipart part = (Multipart) message.getContent();
MimeBodyPart mimePart = null;
for (int i = 0; i < part.getCount(); i++) {
mimePart = (MimeBodyPart) part.getBodyPart(i);
System.out.println(mimePart.getContent());
System.out.println("========");
}
emailFolder.close(false);
st.close();
}// catch (NoSuchProviderException e) {e.printStackTrace();}
catch (MessagingException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
`
I am calling receive email from a driver class. This is literally the only method being called.
I will run the code and it will work fine, printing the contents of the 229th email. Then, if i run it again, with not changes whatsoever, I get an array index out of bounds exception saying index 229 is out of bounds for length 229. As I understand it, when i run it, the code works fine, but somehow the email I want to access is no longer accessible. I have been trying to access the last email for testing purposes and I knew which number it is. What I think is happening is that the code is making the email I just accessed unavailable. I just added a print line and confirmed that when i first run it, the length is such that the message being accessed is the last element in the array, upon running the program again, the array is mysteriously one element smaller. I have verified that the emails are actually not disappearing from my account. I think i started at email 250 or something and have been gradually losing access. Another interesting thing to note is that yesterday, I was working with a different email account that only had 19 emails in it, after a short period of time, I was suddenly unable to list out the emails in that account with code. The emails were there, I could send emails with code, but not access the emails stored on that account.
In catch block of sending mail with Javamail API I'm using instanceof to categorize exceptions AuthenticationFailedException is doing its job But SendFailedException is not. I'm testing my application with some mail addresses that include some valid and some invalid addresses But SendFailedException do not trigger on invalid addresses.
Here is how I'm using catch block
catch (MessagingException ex) {
if (ex instanceof AuthenticationFailedException) {
JOptionPane.showMessageDialog(null, "Exception: \n" + "Athentication failed due to invalid sender mail or password\n");
} else if (ex instanceof SendFailedException) {
SendFailedException e = (SendFailedException) ex;
final Address[] invalidAddresses = e.getInvalidAddresses();
setFailedAddresses(invalidAddresses);
final Address[] validSentAddresses = e.getValidSentAddresses();
setConfirmedAddresses(validSentAddresses);
final Address[] validUnsentAddresses = e.getValidUnsentAddresses();
resend(validUnsentAddresses);
JOptionPane.showMessageDialog(null, "Exception: \n: " + "invalid mail address = "+invalidAddresses.length+"\nvalid addrs = "+validSentAddresses.length+"\nvalid not treated = "+validUnsentAddresses.length);
} else if (ex instanceof NoSuchProviderException) {
JOptionPane.showMessageDialog(null, "Exception: \n " + "No provider found with the name you specified");
Can you point out what where I'm wrong?
My application runs fine and send mails to valid addresses But exception not arise on invalid addresses.
Actually program do not enter into this exception.
Here is how I add Recipients
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(addrsString));
Where: addrsString is a string containing comma separated mail.
What I want is it should add invalid addresses to an array an shown in exception code that it is not doing right now.
I've been making a instant chat program, and wanted to make it possible for users to "whisper" or private message one another. The way I implemented that is the user would type:
/w [username] [message]
I then send it to the server which sends it to all the users. The users then have to check to see if its sent to them, this is that method:
if (message.startsWith("/msg/")) {
message = message.trim();
message = message.substring(5);
String[] words = message.split("//s");
String UserName = null;
try {
String username = words[2];
UserName = username;
System.out.println(UserName);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error reading the whisper command!");
}
if (UserName == client.getName()) {
List<String> list = new ArrayList<String>(Arrays.asList(words));
list.removeAll(Arrays.asList(2));
words = list.toArray(words);
String text = words.toString();
text = "You've been whispered! " + message;
console(text);
}
Everytime I send a /w when I'm testing it always give the ArrayIndexOutOfBoundsException. I also modify the message in the sending. Heres that method:
else if (message.toLowerCase().startsWith("/w")) {
message = "/msg/" + client.getName() + message;
message = "/m/" + message + "/e/";
txtMessage.setText("");
}
I also added a whole bunch more options for the actual code for the users, I made it /whisper, /m, /msg, and /message, but those are all just copies of this with a different input. Why is it giving me an ArrayIndextOutOfBoundsException, when the 3rd place in the words array SHOULD be the username that the sender is trying to send it to. Obviously this probably isn't the best way to send private messages, and if any of you guys have a simpler way I can implement to my server, please go ahead and let me know! Just know that I am a young, new programmer and so I will probably have a lot of questions.
The slashes in your split() regex are backwards. You need
String[] words = message.split("\\s");
You can also just use a space
String[] words = message.split(" ");
I'm using a code to recover passwords. when a user requested to reset password, it send a SMS to user's phone. my problem is that it gives me this output in the middle of the runtime.
===============================================================================
Now Sleeping - Hit enter to terminate.
but I want to send the enter signal automatically so the job will be terminated automatically.how can I do this?
My code is below:
if (rs3.next()) {
String port = "COM3"; //Modem Port.
int bitRate = 460800; //this is also optional. leave as it is.
String modemName = "Huawei E165G"; //this is optional.
String modemPin = ""; //Pin code if any have assigned to the modem.
String SMSC = "+947100003"; //Message Center Number ex. Mobitel
GsmModem gsmModem = new GsmModem();
GsmModem.configModem(port, bitRate, modemName, modemPin, SMSC);
gsmModem.Sender(mobileno, "Your new password is "+rs3.getString("password")+" Please change your password in your next login. >>Fast Inventory<<"); // (tp, msg)
System.out.println("Code came here");
Statement s2=Db.connectDb().createStatement();
s2.executeUpdate("UPDATE user SET lockstatus='0' WHERE uid='"+username.getText()+"'");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
I'm using SMSlib.
I am trying to access my gmail account and retrieve the information of all unread emails from that.
I have written my code after referring many links. I am giving a few links for reference.
Send & Receive emails through a GMail account using Java
Java Code to Receive Mail using JavaMailAPI
To test my code, I created one Gmail account. So I received 4 messages in that from Gmail.
I run my application after checking number of mails. That showed correct result. 4 unread mails.
All the infomation was being displayed (e.g. date, sender, content, subject, etc.)
Then I logged in to my new account, read one of the emails and rerun my application.
Now the count of unread message should have been 3, but it displays "No. of Unread Messages : 0"
I am copying the code here.
public class MailReader
{
Folder inbox;
// Constructor of the calss.
public MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, "abc#gmail.com",
"paasword");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
public static void main(String args[]) {
new MailReader();
}
}
I searched on google, but I found that you should use Flags.Flag.SEEN to read unread emails.
But thats not showing correct results in my case.
Can someone point out where I might be doing some mistake?
If you need whole code, I can edit my post.
Note: I edited my question to include whole code instead of snippet I had posted earlier.
Your code should work. You can also use the Folder.getUnreadMessageCount() method if all you want is the count.
JavaMail can only tell you what Gmail tells it. Perhaps Gmail thinks that all those messages have been read? Perhaps the Gmail web interface is marking those messages read? Perhaps you have another application monitoring the folder for new messages?
Try reading an unread message with JavaMail and see if the count changes.
You might find it useful to turn on session debugging so you can see the actual IMAP responses that Gmail is returning; see the JavaMail FAQ.
You cannot fetch unread messages with POP3. From JavaMail API:
POP3 supports no permanent flags (see Folder.getPermanentFlags()). In
particular, the Flags.Flag.RECENT flag will never be set for POP3
messages. It's up to the application to determine which messages in a
POP3 mailbox are "new".
You can use IMAP protocol and use the SEEN flag like this:
public Message[] fetchMessages(String host, String user, String password, boolean read) {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore();
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
// use READ_ONLY if you don't wish the messages
// to be marked as read after retrieving its content
emailFolder.open(Folder.READ_WRITE);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, read);
return emailFolder.search(unseenFlagTerm);
}
Another thing to notice is that POP3 doesn't handle folders. IMAP gets folders, POP3 only gets the Inbox. More info at: How to retrieve gmail sub-folders/labels using POP3?
Change inbox.open(Folder.READ_ONLY); to inbox.open(Folder.READ_WRITE);
It will change your mail as read in inbox.
Flags seen = new Flags(Flags.Flag.RECENT);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
messages = inbox.search(unseenFlagTerm);
The correct flag to use is
Flags.Flag.RECENT
please use this method to get the unread mails
getNewMessageCount()
refer below link:
https://javamail.java.net/nonav/docs/api/com/sun/mail/imap/IMAPFolder.html