I need to send a group mail as a part of batch job. I'm using javax.mail package to accomplish this.
Properties m_properties;
m_properties = new Properties();
m_properties.put("mail.smtp.host", "localhost");
m_properties.put("mail.smtp.port", Integer.toString(26));
Session m_Session = Session.getDefaultInstance(m_properties);
Message m_simpleMessage = new MimeMessage(m_Session);
InternetAddress m_fromAddress = new InternetAddress("me#sample.com");
InternetAddress m_toAddress = new InternetAddress("group#sample.com");
m_simpleMessage.setFrom(m_fromAddress);
m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
m_simpleMessage.setSubject(m_subject);
m_simpleMessage.setContent(m_body, "text/html");
Transport.send(m_simpleMessage);
I'm using a windows server. I installed IIS SMPT server and using it. I have no problem when I send a mail to individual id. But I get an error when trying to send to a group.
Error :
This is an automatically generated Delivery Status Notification.
Delivery to the following recipients failed.
group#sample.com
Do I need to configure something in my SMPT server for a group, or do I need to make changes in my code for allowing to send a group mails.
The group must exist in the destination server... Does the destination smtp server you are reaching contains the group?
Related
I have been trying lot of things from multiple resources like Read Inbox Java2s
and How to get the list of available folders in a mail account using JavaMail
I am sending emails succesfully, but to be sure of mail is sent successfully, i need to read emails from sent items folder Is this possible with smtp? if yes, how?
Currently I am stuck even in connceting with stroe. I am finding no way to pass the steps
Store store = session.getStore(); and store.connect();
I have no idea about imap or pop3. They might not have been even configured at our server, but if smtp does not faicilitate then I am ready to process with those protocols, though I am sending mails using stmp yet. I have tried lot of edits in my following code, but nothing is helping
String host = "mysite.smtp.com";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "myport");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
String user = "myname";
String password = "mypassword";
return new PasswordAuthentication(user, password);
}
});
Store store = session.getStore(); // had tried writing "imaps" here
store.connect(host, null, null);
//store.connect(); also tried this
Folder inbox = store.getFolder("INBOX"); //actually i need "SENT"
if (inbox == null) {
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
System.out.println("Message " + (i + 1));
messages[i].writeTo(System.out);
}
inbox.close(false);
store.close();
You can't use smtp to read email, you need to use imap. pop3 won't help because it only lets you read the Inbox.
Depending on what you mean by "sent successfully", a successful return from the send method will tell you what you want to know.
Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)
If what you really want to know is that the message was successfully delivered to the destination mail server(s) and all the addresses were valid, that's harder. The JavaMail FAQ has more information.
I know that I am answering late. I encountered quite similar problems while maintaining a software using extensively mail sending functionalities. I finally created a JUnit extension to write integration tests with a SMTP server emulation.
Please have a look to github.com/sleroy/fakesmtp-junit-runner/. By the way it's open-source.
Refer this link:
How to save sent items mail using the Java mail API?
Sent Mail Service using JAVAMAIL API
Note also that depending on the mail server you're using, sent messages don't automatically appear in a "Sent Messages" folder; you might need to copy them their yourself after sending the message. (Gmail does this automatically, but not all other servers do.)
I'm trying to read messages from IMAP folders using javax.mail, but after fetchig few messages Im getting following exception:
* 20 FETCH ()
DEBUG IMAPS: ignoring bad response, THROW:
com.sun.mail.iap.ParsingException: error in FETCH parsing, unrecognized item at index 12, starts with ")
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:219)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:392)
at com.sun.mail.iap.Protocol.command(Protocol.java:354)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2113)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2094)
at com.sun.mail.imap.IMAPFolder.fetch(IMAPFolder.java:1253)
at app.mail.imap.ImapClient.synchronizeLocalData(ImapClient.java:563)
at app.mail.imap.ImapClient.lambda$6(ImapClient.java:351)
at java.lang.Thread.run(Thread.java:745)
* BYE Internal error occurred. Refer to server log for more information. [2016-11-03 21:20:44]
This is the code Im using to read messages (session, store and folder are already opened):
final Message[] messages = imapFolder.getMessagesByUID(1, IMAPFolder.LASTUID);
final FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
imapFolder.fetch(messages, fp);
for (final Message msg : messages)
{
final long uid = imapFolder.getUID(msg);
final String subject = MimeUtility.decodeText(msg.getSubject());
final Date date = msg.getReceivedDate();
info("MESSAGE: {} -> {} on {}", uid, subject, date);
}
It seems it's happening only on very specific messages and only when fetching more informations than message UID (if Im reading only uid then code works).
As far as I know many IMAP servers implementations lack many features from IMAP protocol. Bearing that in mind, what is safest and most reliable method for fetching and reading messages from IMAP folders?
javax.mail version: 1.5.3 (provided by wildfly app server 10.0.1.Final).
Thanks for your help. It seems it is indeed IMAP server error. When I'm opening message in web client i get:
Connection error (#022).
Also KMail do not download messages for that folder (I bet other clients too).
When I receive mails, open the inbox, and get the messages; how can I compare them to a previous array of messages, so that I know which of them I have already read?
Properties properties = new Properties();
properties.put("mail.imaps.host", host);
properties.put("mail.imaps.port", "993");
Session emailSession = Session.getDefaultInstance(properties);
//2) create the POP3 store object and connect with the pop server
Store emailStore = emailSession.getStore("imaps");
emailStore.connect(user,pw);
//3) create the folder object and open it
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
How to handle this depends on exactly what your requirements are. For example, will other applications and/or users be accessing the mailbox at the same time as your application? Or does your application have dedicated access to the mailbox?
You can use the SEEN flag to find messages that you haven't already read.
You can also save the IMAP UID for the last message you processed (and the UIDVALIDITY for the folder) to later find messages with larger UIDs. See the UIDFolder interface for details.
(The comments in your code talk about pop, but your code is using imap; if you ever have to use the pop3 protocol the answers are quite different.)
I am trying to read the UNREAD mails from gmail account. I tried to filter the messages based on the Flags, but it did not work. I printed the Flags sent on each message, but nothing is set on it, because of which I could not filter the messages. I used the keyword Flags.Flag.SEEN to filter the messages.
After googling, I found out that it is something with the email client. For ex, we need to the change the configuration in gmail or any exchange mail server. Can you please tell me on what to change the configuration to read the UNREAD mails?
Also, end of day, I am going to implement the code into one of the smtp exchange servers. Please let me know if there needs a special configuration. So that I can inform the respective team and then implement my change.
// connects to the message store
Store store = session.getStore("pop3");
store.connect(userName, password);
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
System.out.println("unread count - " + folderInbox.getUnreadMessageCount());
folderInbox.open(Folder.READ_WRITE);
// folderInbox.search(new FlagTerm(new Flags(Flags.Flag.RECENT),
// false));
// fetches new messages from server
Message[] arrayMessages = folderInbox.search(new FlagTerm(new Flags(Flags.Flag.RECENT), false));
The POP3 protocol doesn't support any flags. Use IMAP.
See Retrieving all unread emails using javamail with POP3 protocol.
One comment suggests the use of Flags.Flag.SEEN for Gmail. There's another suggestion about changing settings in your test Gmail account.
I have an Erlang server which is spawning a new process for each client that connects. Then the Pid of this new process is passed to the client (to make a connection to the new process.) Is that enough to make a connection from a jinterface client?
I am using this to connect from the client first:
final String SERVERNAME = "server";
final String SERVERNODE = "bertil#computer";
mbox.send(SERVERNAME, SERVERNODE, connectClient);
And those names is set in the server when it starts:
start() ->
net_kernel:start([bertil, shortnames]),
register(server, self()).
Do I have to register a new name for each spawned process? That would not be so dynamic... How do I solve this? Should I use the main process at the server as a router to send all traffic through?
Once you have a pid, you should be able to send a message directly to it. In Erlang you don't have to specify a node if you got a pid. You only need a node if you are sending to a registered name since names are only unique per nod. Pids are unique in the whole cluster.
If you have a varable my_pid as an OtpErlangPid object you can send like so:
mbox.send(my_pid, message);
See the documentation for the send function and chapter 1.6 Sending and Receiving Messages in the Jinterface User's Guide.