I have setup mail server mail.example.com at smtp. Trying to send email with that, and the mail server send the mail correctly.
But if the email bounces, I want them to read at another email address, thats one gmail address. So for that i have tried doing this
SMTPMessage sMTPMessage = new SMTPMessage(mailSession);
sMTPMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
sMTPMessage.setFrom(new InternetAddress(from));
sMTPMessage.setSubject(subject);
sMTPMessage.setContent(body, contentType);
sMTPMessage.setEnvelopeFrom(bounceAddr);
SMTPTransport smtpTrans = new SMTPTransport(mailSession, new URLName("smtp", smtpServer, port, null, userid, password));
smtpTrans.connect();
smtpTrans.sendMessage(sMTPMessage, sMTPMessage.getAllRecipients());
System.out.println("transport "+smtpTrans.getReportSuccess());
smtpTrans.close();
But it didn't worked. and so I am not getting the bounce email information. Also I tried by setting mail.smtp.from property that also didn't worked.
Also I saw this
props.put("smtp.mail.from", bounceAddr);
But the from email address is not at the mail server, its on my gmail address. So is there any issue with from address domain ?
If you're using properties, make sure you don't have a typo in the property name, as you do above.
You can make sure that JavaMail is using the envelope From address that you specify by turning on JavaMail session debugging and looking for the MAIL command in the SMTP protocol in the debug output.
But the real problem is likely one of:
Not all mail servers honor this information, even though they
should.
One of the mail servers starting with your mail server and through to the destination mail server may not have forwarded this information as it forwarded your message, thus leaving the mail server that detects the problem without the information to return the error message to the proper place.
Your mail server may not be honoring this information because it's not an email address in the domain managed by your mail server.
Some of these problems may be fixable by checking the configuration and behavior of your mail server. But in the end you simply can't rely on always getting an error message back when you send to an invalid address.
You can find more information in the JavaMail FAQ.
Related
I have a Java app that sends email via Apache Commons Email. The app works just find on my development environment, but when I deploy it to the server, I'm getting an error that Commons couldn't connect to the email server.
org.apache.commons.mail.EmailException: Sending the email to the following failed : 255.255.255.255 :587
...Error authenticating with server.
Just to be sure this wasn't my configuration, I've tried on 2 different email providers, 1) Our email company email provider 2) Gmail. Both work on my dev and both fail with the same message on the server.
I've tried several ports and IP combinations. I've turned SSL on and off. I've checked with our email provider. I'm left with a configuration of either the server or the network.
I work remotely so I'm not on the same network as our servers, but I the app works for me even if I am on VPN.
I've used telnet from within the server and I can connect to the email provider on the proper port.
Does anyone have anything else I can try?
Thanks.
Edit
The error I'm getting is an authentication error. Could there be a security setting to prevent the credentials from being passed?
Still sounds like a permission problem. Ports below 1024 are restricted. Start your app with sudo permission.
It appears I was missing 1 line of code.
Assume "email" is my mail object...
email.setSSL(true);
All is working now. I'm not sure why it wasn't working before.
I am using apace common mail API for sending html emails. following is my code.
public void sendHTMLMail(String to, String subject, String message , String from) throws EmailException
{
HtmlEmail email = new HtmlEmail();
email.setHostName(SMTP_HOST_NAME);
email.addTo(to);
email.setFrom(from, "just-flick");
email.setSubject(subject);
email.setSmtpPort(25);
email.setHtmlMsg(message);
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
But while running the program I am getting following error.
Exception in thread "main" org.apache.commons.mail.EmailException: Sending the e
mail to the following server failed : mail.just-flick.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at bseller.mail.SendMail.sendHTMLMail(SendMail.java:105)
at bseller.mail.SendMail.main(SendMail.java:31)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 550 Access denied - Invali
d HELO name (See RFC2821 4.1.1.1)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 3 more
Please help me to configure this problem.
Thanks
I've faced the same problem, and when i pass the name of client host everything became ok
i've add this line in my code:
props.put("mail.smtp.localhost", client or host name which connect to mail server);
Good Luck :)
This should be handled by the administrator of your mail server, not by you. Talk to whoever told you to connect to that mail server.
Maybe this will help:
email.getMailSession().getProperties().setProperty("mail.smtp.localhost", "www.example.com");
Of course replace www.example.com with the domain name of your host from where you send the mail.
However, as David Schwartz wrote, your mail configuration is not perfect either. Nowdays we do not submit mails to port 25. Port 587 is used for submission, which has more relaxed rules, although it may be necessary to authenticate yourself if your IP address is not white listed. Follow the link which was given by pst in his comment.
It is strange that JavaMail alone works, while Apache Commons Email does not, because I guess Commons Email also used JavaMail. This may indicate a bug somewhere but it would require further investigation.
One of your problem is that you do not know the exact HELO name you are sending. The following code may help to determine it, otherwise call your mail administrator (especially because he may advise you on port 587).
email.getMailSession().setDebug(true);
I was getting same error, it was fixed after enabling below properties:
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
I am trying to send an email using Apache commons Email API.
I installed hMailServer as my smtp server and created a domain test.com. I added an user, 'user1' .
I tried to send the mail using the below code
public static void sendSimpleMail() throws Exception {
Email email = new SimpleEmail();
email.setSmtpPort(25);
email.setDebug(false);
email.setHostName("localhost");
email.setFrom("user1#test.com");
email.setSubject("Hi");
email.setMsg("This is a test mail ... :-)");
email.addTo("abc#gmail.com");
email.setTLS(true);
email.send();
System.out.println("Mail sent!");
}
When my program runs, it prints , "Mail sent!". But it has been about 30 minutes, but I haven't got the mail in my inbox.
Is there something I am missing ? Is there delay due to network problems ?
Update:
I ran diagnostics and I got the below details.
I think the problem might be with outbound port.
Can anyone help me figure what is going wrong?
There could be a number of problems. Since you didn't get an exception in your Java code, most likely the E-mail has reached your hMailServer instance, but hasn't gotten past that. The documentation for hMailServer includes a number of troubleshooting suggestions.
My best guess would be that your ISP (or your local firewall) is blocking outbound port 25. Also, be sure to check your spam folder on gmail.
Well take a look at the error: "mail.hmailserver.com could not be resolved" (AKA not found). Are you sure that's the correct address? Are you sure there's a mail server there?
BTW unless you have some HOSTS file enteries, that second failed test with "test.com" will never work. Use a real mail server
I am using mailapi.jar to sene mail.i am using gmail smtp details.i am setting authentication for that session.
i also setting from address in setfrom() method.
i can receive mail..but that from address was wrong .
for Example.
I am setting from address like from#gmail.com
To address like to#gmail.com
i can receive the mail in to#gmail.com id.
But the from address is my authentication id.not the from#gmail.com
that may be enforced by the mail server. decent mail servers don't generally let you impersonate anyone you like.
Our website was created with Google Apps.
My website URL is http://mydomain.com, which is pointing to http://sites.google.com/a/mydomain.com/.
I want to send email to a registered user.
I am using the Apache Commons jar to send mail to the user.
I get this error message:
Sending the email to the following server failed : http://mail.google.com/a/mydomain.com:25
I used smtpPortAddress=25 & hostName=http://mail.google.com/a/mydomain.com.
I am not spending money on email server setup, so I want to send email through our website.
Which hostname is required to send mail?
I do not know if you can use port 25, for the encrypted SMTP port you can use
Host: smtp.gmail.com
Port: 465
Authentication: Yes
Username: you#yourdomain.com
Password: #YOUR_PASSWORD#
Security: SSL
Either way, just use smtp.gmail.com with your #yourdomain.com name.
You could use any mail server to sent your mail.
But if you like to use GMail the server is smtp.gmail.com.
But you also will have to make sure you authenticate as a valid gmail user.