external email not working exchange and javamail - java

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.....

Related

Java to *get* mail from SMTP sevrer

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.

Cannot send email without authentication required

I wrote a simple Java program that uses Java Mail API to send an email.
public static void main(String[] args) {
System.out.println("SimpleEmail Start");
String smtpHostServer = "smtp.gmail.com";
String emailID = "xxxxxx#hotmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHostServer);
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, null);
EmailUtil.sendEmail(session, emailID,"SimpleEmail Testing Subject", "SimpleEmail Testing Body");
}
}
EmailUtil class:
public class EmailUtil {
/**
* Utility method to send simple HTML email
* #param session
* #param toEmail
* #param subject
* #param body
*/
public static void sendEmail(Session session, String toEmail, String subject, String body){
try
{
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("no_reply#example.com", "NoReply-JD"));
msg.setReplyTo(InternetAddress.parse("no_reply#example.com", false));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
With this implementation, it is said I do not need any password at all so i gave this a tried and it tells me:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required
I got this code from an online resources and from what I read,it should be able to send without authentication.
So my question is, do I always need to set a username and password to send mail using JAVA mail?
If no, what am I doing wrong?
It depends on who's running the server.
See https://support.google.com/a/answer/176600?hl=en&vid=0-974788924023-1554173451081; specifically the column labeled 'Gmail SMTP server' (which you're using). Google explicitly says that you have to authenticate to utilize that server. Not doing so gives you this error message.
Once upon a time you could use any mail server to send emails to any email address you wanted.
And then came SPAM. Spammers also could use any mail server to send emails to any email address - and as a mail server operator you do not want spammers to use your server for sending emails (because operating the server costs you money, because your mail server can get blacklisted for spamming).
So today most mail servers require that you
either provide authentication (for sending emails to any email address you want)
or are only allowed to send emails to email addresses hosted by the mail server operator
Google even has two distinct mail servers:
one that requires authentication, that can be used for sending emails to any email address you want (that is the server at smtp.gmail.com)
one that doesn't require authentication, that can only be used for sending emails to Gmail or G Suite users (that is the server aspmx.l.google.com)
It could be that your source dates back to a time when no authentication was required or that the mail server in your source was only used for sending mails to addresses that are hosted at the mail server.
Either way - if you want to use the server smtp.gmail.com for sending mails to any address you must authenticate (or convince google that they should allow you - and only you - to sending emails without authentication, but then: how will google know that it is exactly you who is trying to send mails?)

AppEngine Email : Unauthorized Sender

When I try to send mail I get a "Unauthorized sender" exception
javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Illegal Arguments (java.lang.IllegalArgumentException: Unauthorized Sender: Unauthorized sender))
at javax.mail.Transport.send(Transport.java:163)
at javax.mail.Transport.send(Transport.java:48)
My code to send mail is very simple:
Session session = Session.getDefaultInstance(new Properties(), null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admingae#tecurti.com", "Adming"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("name#gmail.com", "Felipe"));
msg.setSubject("Assunto", "UTF-8");
msg.setText("texto corpo");
Transport.send(msg);
I´ve already give to admingae#tecurti.com "owner" permission on admin console.
Here is my App Engine Console permission
If anyone could help me I appreciated
thanks
Solutions
Thanks to Andrei Volgin I will register the solution
Admin Console Correct Register
In console go to App Engine > Settings > Application Settings. Add this email address to the list of authorized senders.
Today this is little different and requires more setup for security reasons.
The zero configuration way, is sending email from an email address with this format:
[anything]#[project_id].appspotmail.com
FYI: https://cloud.google.com/appengine/docs/standard/java/mail/#who_can_send_mail

Cannot send email using Java code on Windows Server 2008 r2?

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.

Issue sending email using gmail's smtp server and id of my website

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.

Categories

Resources