I have some code below:
Properties props = System.getProperties();
//set mail protocol
//props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.store.protocol", "pop3s");
//create new store
Session session = Session.getDefaultInstance(props);
//store = session.getStore("imaps");
store = session.getStore("pop3s");
store.connect("pop.mail.yahoo.com", 995, "MyYahooUserName", "MyPassword");
//store.connect("imap.next.mail.yahoo.com", 993, "MyYahooUserName", "MyPassword");
isConnect = true;
It's work when I try IMAP protocol, but with POP3 I got exception below and I don't know why
SEVERE: null
javax.mail.AuthenticationFailedException: EOF on socket
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:208)
at javax.mail.Service.connect(Service.java:295)
at gmailutilities.GmailUtilities.connect(GmailUtilities.java:88)
at gmailutilities.Main.main(Main.java:29)
javax.mail.MessagingException: Not connected
at com.sun.mail.pop3.POP3Store.checkConnected(POP3Store.java:408)
at com.sun.mail.pop3.POP3Store.getDefaultFolder(POP3Store.java:357)
at gmailutilities.GmailUtilities.openFolder(GmailUtilities.java:103)
at gmailutilities.Main.main(Main.java:30)
*Any one know why? Plz help me. Thanks all! *
Note that free Yahoo! Mail accounts do not allow POP3 or SMTP access. You must purchase a Yahoo! Mail Plus account to get POP3 and SMTP access.
Related
I'm trying to get an email from exchange SMTP server (port 25).
all the examples i saw are to send an email with SMTP while i would like to get (read) an email.
I wrote a code using JAVAMail that get email with impas that work perfect but in the last mooment the demand change to use SMTP for incoming mail.
Java code for IMAP incoming mail
public void getAttachment() throws MessagingException, IOException {
properties.setProperty("exchange server host",host);
properties.put("smtp.gmail.auth", "true");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
}
);
Store store = session.getStore("imaps");
try {
logger.info(String.format("Going to get connection to exchange server %s for user %s " ,host, user));
store.connect(host, user, password);
}
catch (MessagingException ex){
logger.error(String.format("Unable to connect exchange server {}", host) + ex.getMessage());
logger.error(ex.getStackTrace());
}
Folder inboxFolder = store.getFolder("inbox");
inboxFolder.open(Folder.READ_WRITE);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message[] message = inboxFolder.search(unseenFlagTerm);
Can someone please advise for getting mail with SMTP protocol and not with IMAP?
can it be done ?
Thanks.
You can't.
SMTP is for a client to send email to a server.
In order for a client to receive email from a server, you need to use protocols like POP3 (Post Office Protocol) or IMAP (Internet Message Access Protocol).
Actually, the Wikipedia page for SMTP says as much:
User-level email clients typically use SMTP only for sending messages to a mail server for relaying, typically submit outgoing email to the mail server on port 587 or 465 as per RFC 8314. For retrieving messages, IMAP and POP3 are standard, but proprietary servers also often implement proprietary protocols, e.g., Exchange ActiveSync.
Good morning to all,
I was try send external email in my organization does not working for me.When i was try internel email address it working fine.. I am getting exception "invalid mail address 5.1.7 unable to relay"
I am also try send with external email via TELNET. Its working fine in TELNET.
Exchange 2013 receive connector anonymous relay was enable.
Properties props = System.getProperties();
props.put(SMTP_HOST_KEY,smtphost);
Session session=null;
System.out.println("Entering into sendSMTPMail=============(props)"+props);
MailAuthentication authorization =new EmailManager().new MailAuthentication();
LogWriter.log("CREATING THE DEFAULT SESSION");
System.out.println("Entering into sendSMTPMail=============(AUTHORIZATION)"+AUTHORIZATION);
System.out.println("Entering into sendSMTPMail=============(authorization)"+authorization);
System.out.println("Entering into sendSMTPMail=============(session)"+session);
if(AUTHORIZATION)session = Session.getInstance(props, authorization);
else session = Session.getInstance(props, null);
session.setDebug(debug);
System.out.println("Entering into sendSMTPMail=============(session)"+session);
OUTPUT in java :
Send is OK
rcpt is Invaild mail address 5.1.7 unable relay
OUTPUT in TELNET:
Send is OK
rcpt is OK
Help is needed......Please let me know what is problem in javacode or exchange 2013
Thanks.....
I have installed SMTP server and IIS Web server on windows 2008 r2 server. I am trying to send a test email using java code through localhost but i am unable to send an email i get the following error not sure what is that i am doing wrong. Apart from installing the SMTP server is there any setting i need to do because i just installed my smtp server and expecting that this code works?
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for marshell#gmail.com
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at LotusNotes.SendEmail.main(SendEmail.java:30)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for marshell#gmail.com at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)
Java Code:
public static void main(String[] args) {
String to = "marshell#gmail.com";
String from = "imrmsmtpmail";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Subject Line!");
message.setText("Test email!");
Transport.send(message);
System.out.println("Sent message successfully....");
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
Probably you have to authenticate to send emails to this server.
AFAIK, you aren't providing any user or password in the connection to the server.
I use something like this:
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.auth", true);
properties.setProperty("mail.user", "PUT AN USERNAME HERE");
properties.setProperty("mail.password", "PUT A PASSWORD HERE");
Session session = Session.getDefaultInstance(properties);
And to make sure you have all the parameters working properly before writing the program it is useful to make a telnet to the mail port (25) to make sure you can send an email directly writing the codes into the server.
In the following link you have an example:
http://support.microsoft.com/kb/153119/en
Altough it may sound extremely technical, it worth to try to make sure that the server send emails from your machine with the given parameters: username (or not username at all), destination address, etc.
I am trying to access an exchange server to read emails from a specific acount using JAVA mail.
I can access gmail with something like
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
store = session.getStore("pop3");
store.connect(host, username, password);
but since I am using exchange I dont have pop3,
I only have server name : mysrv ,domain name: MYDOMIAN and a mailbox : my#mail.co.il.
So what is the correct way to connect to exchange?
you may try
Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);
store = session.getStore("imap");
store.connect(host, username, password);
IMAP4 won't give you contacts/appointments/tasks/etc.
You can use EWS, Outlook Object Model (assuming you have Outlook installed and a profile configured to talk to a particular mailbox) or Redemption (I am its author) and its RDOSession object(RDOSession.LogonExchangeMailbox etc.)
I have created an email account for admin of my web site using google's gmail app as admin#mywebsite.com. I am able to login using the credentials on gmail's login web page.
Now i want to send email using java and this id
String host = "smtp.gmail.com";
String from = "admin#mywebsite.com";
String pass = "myPass";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
....
...
...
Session session = Session.getDefaultInstance(props, auth);
...
...
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
This is throwing an exception as
Exception in thread "main" javax.mail.AuthenticationFailedException
However, when i try this with gmail credentials (myid#gmail.com), it works.
Please help me out on this. Thanks in advance
Judging by the accepted answer to What mechanism does Gmail use for user authentication?, you need to connect to Google's mail server on port 465 or 587, then use STARTTLS (presumably only on port 587) and user authentication. So what you are lacking is the port 465 or 587 part.
Have you already authenticated your admin#mywebsite.com with Send As permissions in your gmail Account Settings?
If yes, then you might need to give your authentication user id and password as your gmail account.
you have to used for port number to connect with gmail.
TLS:587
SSL:465
you have to used one of them so you can send the email through gmail.