I am using XAMPP for deploying my java application in tomcat and also using mercury mail to ssend emails. Now i am just testing my application with a small java program using java mail API and mercury email. I havee done the necessary configuration in Mercury to setup localhost. My program is running successfully without any error. Also Mercury log file doesn't say anything about any error.
T 20130411 044359 51663963 Connection from 127.0.0.1
T 20130411 044359 51663963 EHLO 10.226.44.101
T 20130411 044359 51663963 MAIL FROM:<promil#localhost.com>
T 20130411 044359 51663963 RCPT TO:<*****#gmail.com>
T 20130411 044359 51663963 DATA
T 20130411 044359 51663963 DATA - 22 lines, 689 bytes.
T 20130411 044359 51663963 QUIT
T 20130411 044359 51663963 Connection closed with 127.0.0.1, 0 sec. elapsed.
Also this is my java file....
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "****#gmail.com";
// Sender's email ID needs to be mentioned
String from = "promil#localhost.com";
// Assuming you are sending email from localhost
String host = "localhost";
String password = "****";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Setup mail server
properties.setProperty("mail.smtp.password", password);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "C:/Users/toshiba/Desktop/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart );
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
I am absolutely clueless about it.....
Also my Mercury core process says it has 4 pending outgoing jobs....???
The destination address is at gmail. You haven't clarified if the mail was supposed to be delivered locally or at gmail, so I'm going to assume you meant the mail to go to a Gmail account, which you've (correctly) obfuscated in your post.
The session transcript you posted is between your Java client and the local Mercury mail server. All this says is that the local Mercury mail server accepted the mail from your Java client.
The session transcript contains no information about what happened to the mail after your local Mercury mail server accepted it. If the local configuration was correctly set up, then Mercury mail should have tried to forward the mail by looking up the MX record for Gmail and connecting to one of the servers returned by the MX lookup.
For further information you will have to look at the Mercury server's logs to see if it attempted to deliver the message.
A guess:
Your Mercury mail server attempted to deliver the mail, but Gmail rejected it because the computer you are running on has an ISP-assigned DHCP (dynamic) address. A large volume of SPAM originates from such addresses, and many mail hosts refuse to even talk to these mail sources. In any event, if Gmail rejected it, Mercury mail is required to "bounce" it back to the sender. However, you probably have not set up an incoming mailbox for promil#localhost.com, so it had no place to store the bounce and just discarded it. Again, check the Mercury mail logs for details.
That's the best answer anyone can give based on the information you have provided.
Related
I try to run a small Programm that sends an generic Email to my Account
But I get an Exception:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 553 We do not relay non-local mail, sorry.
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1873)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.main(SendEmail.java:54)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 553 We do not relay non-local mail, sorry.
at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1724)
... 4 more
This is my Code
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "Basti-V#web.de";
// Sender's email ID needs to be mentioned
String from = "Basti-V#web.de";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Im using the XAMPP Control Panel to run a Mercury Server. The Ports are 25,79,105,106,110,143 and 2224. Im new to this so maybe someone can push me in the right direction.
You must uncheck "Do not permit SMTP relaying of non-local mail" option.
See the link: uncheck option
Please check below two things.
1).where the host mail server is running. If it is running in local machine then set the host address as 0.0.0.0
2.If it is external mail server, check the mail credentials like username, password, host.
I think 553 is "denied error" from server, that means you have not provided correct credential .
The code I am currently using is as follows:
String to = "....#gmail.com";
String from = ".....#gmail.com";
String host = "127.0.0.1";
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("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
If I paste the above code in my Java servlet and run it, the following exception gets thrown:
javax.mail.NoSuchProviderException: smtp
I have also tried following the solutions outlined in these resources but to no avail: link1 link2 link3 link4.
It would help if you included the the full stacktrace for the javax.mail.NoSuchProviderException and JavaMail debug output. Because you are running this in a servlet container you could be running into Bug 6668 -skip unusable Store and Transport classes. Change your call to Transport.set to the following:
final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(Session.class.getClassLoader());
try {
Transport.send(message);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
That code will isolate which transport class the code is allowed to see. That will rule out that bug in your code.
NoSuchProviderException means something is messed up in your JavaMail API configuration. Where and how have you installed the JavaMail jar file?
Also, in case it's not clear from the other responses, the only way you're going to be able to send mail without authentication is if you're running your own mail server. You can't do it with general purpose online e-mail services (e.g. Gmail).
First of all, if you wanna use gmail to send emails from your program you need to define stmp host as gmail. Your current host "127.0.0.1" is localhost meaning your own computer? Do you have mail server running on your computer?
Here you can see some tutorial how to send an email using gmail: http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
If you're afraid to pass your normal gmail account details then create some kind of test email like kselvatest or something.
You are missing pretty much those:
final String username = "username#gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
So basicly all you need to add is:
1.login and password so JavaMail can use some gmail account
2.declare which mail server you wanna use
change String host = "127.0.0.1"; to String host = "smtp.gmail.com";
3.set mail port
For this code you should get javax.mail.MessagingException: Could not connect to SMTP host: 127.0.0.1, port: 25; exception
But I suspect you have a jre issue. May be smtp implementation has a problem. Why don't you download java mail implementation from http://www.oracle.com/technetwork/java/javamail/index-138643.html and add it to your project class path and try?
I want to send one mail to specified recipient but my sender can have different mail account like outlook or ymail or gmail.
Is it possible to send an email from different email id to same recipient? I'm using this code :
try
{final String from=request.getParameter("from");String smtpServ="",port="";final String pass=request.getParameter("pass");String to=request.getParameter("to");String subject=request.getParameter("subject");String body=request.getParameter("msg");if(from.contains("#gmail.com")){smtpServ="smtp.gmail.com";port="465";}else if(from.contains("#outlook.com") || from.contains("#hotmail.com")){smtpServ="smtp.live.com";port="587";}else if(from.contains("#ymail.com") || from.contains("#yahoo.com") || from.contains("#rocketmail.com") || from.contains("#yahoo.in")){smtpServ="smtp.mail.yahoo.com";port="465";}Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host",smtpServ);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
Session session1 = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,pass);
}
});Message message = new MimeMessage(session1);message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
message.setSubject(subject);
message.setText(body);
// -- Set some other header information --
message.setHeader("MyMail", "Java Mail Test");
message.setSentDate(new Date());Transport.send(message);System.out.println("Message sent to"+to+" OK."); }
catch (Exception ex)
{ ex.printStackTrace();System.out.println("Exception "+ex); }
Sending mails via Java (using Java Mail API) requires you to setup an SMTP host that would send your email to the recipient after proper log-in validation. I've used Java Mail with Gmail IDs and Gmail as SMTP host server. Some e-mail service providers do not allow sending emails from outside of their own web-services (making them incompatible with Java Mail).
But, in order to support multiple senders, you'll need to do the following:
Change the SMTP server to your correct host as per the email ID in use.
Run the Authenticator, with the new e-mail address and password, every time the sender ID is changed.
Then send your message.
My suggestion would be to store these sender email IDs and passwords (and SMTP hosts) when the program is initially run and then iterate the above steps with required changes, for each email id and password pair.
The Caveat:
In my opinion it's best to stick with a single sender else, multiple messages with similar content, by varied sender addresses, may be marked down as Spam by the recipients email service provider.
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
replace this thing with,
message.setRecipients(Message.RecipientType.TO, Address[] addresses);
[N.B.- addresses are ur desired IDs.]
I have a Mailserver, which is hosted at the same network as my phone, but I keep getting an exception saying Unknow SMTP host. I've read several places that this exception occurs when the hostname isnt correct spelled, of course. But in this case it IS. In C# its done a simple way like this:
var client = new SmtpClient(_strSmtpHost);
client.UseDefaultCredentials = true;
var mailMessage = new MailMessage(_strFrom, emailTo);
mailMessage.Subject = subject;
mailMessage.Attachments.Add(attachment);
client.Send(mailMessage);
And in my application its done this way:
SendMail m = new SendMail("Username", "password");
String[] toArr = {"TO#EXAMPLE.com"};
m.setTo(toArr);
m.setFrom("FROM#EXAMPLE.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");
try {
m.addAttachment(fileName);
if(m.send()) {
Log.i("MAIL SENDER: ", "Succesfully");
} else {
Log.i("MAIL SENDER: ", "UnSuccesfully");
}
} catch(Exception e) {
Log.e("MailApp", "Could not send email" + e);
}
In my SendMail class I define a String containing the DNS address of the host
_host = "napserver"; // default smtp server
_port = "25"; // default smtp port
Before I try to do a connect, I want to Authenticate username and password for the mail-address sending the mail:
Session session = Session.getInstance(props, new GMailAuthenticator(_user, _pass));
and then trying to a connect and send message
Transport transport = session.getTransport("smtp");
transport.connect(_host, 25, _user, _pass);
Transport.send(msg);
Just for testing purposes I tried the SMTP address for Gmail, and this worked, and the mail was sent to the specified mail account. Can anybody give me a hint?
EDIT
I should also mention that I tried to connect via Telnet to this mail server, and the ouput from PuTTy, when connected with the host napserver and port 25 :
220 napserver.nap01.dac.no.eu-admin.net Microsoft ESMTP MAIL Service, Version: 6 .0.3790.3959 ready at Tue, 7 Aug 2012 13:38:55 -0700
EDIT 2
As you can see from the PuTTy output:
Microsoft ESMTP MAIL Service
My mailserver uses the ESMTP, but as far as I know this just implements some security layers and so on. Maybe this is where the error is?
EDIT 3
I tried to access the mailserver via remote desktop, I pinging my phones IP-address just to be sure that the server and the phone is located on the same network, and it is..
Im tring to send a simple email with this code using google app engine.
But nothing happens, is there something i have to configure in order to use the mail api?
This runs on localhost.
I am using gmail as mail host.
String host = "smtp.google.com";
String to = "example#yahoo.fr";
String from = "example#gmail.com";
String subject = "this is a test";
String messageText = "test";
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);
// Set debug on the Session
// Passing false will not echo debug info, and passing True will.
mailSession.setDebug(sessionDebug);
// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
When running the AppEngine development server locally, anything sent via the Mail service will not actually be sent - it will just be logged to the console
See here
When an application running in the development server calls the Mail service to send an email message, the message is printed to the log. The Java development server does not send the email message.
In addition, the from address must be (from here)
The email of an app administrator
The email of the currently logged in user who signed in using a Google Account
A valid email receiving address from the app
The sender should be your own Gmail email address instead of example#gmail.com
Reason is because the SMTP server needs to authenticate you.
Apparently, GAE doesn't allow the use of the admin accounts any more. you need to use the service account: project-id#appspot.gserviceaccount.com
My previous projects still work with admin accounts, but the recently created projects just don't allow me to use any of the admin accounts.
Other than email not working on localhost or due to the sender email not being the authenticated one, I have experienced that email does not work even when the version is not the default one. I could not find this documented anywhere.
For example:
nondefaultversion-dot-myapp.appspot.com (email does not work, no error logs)
myapp.appspot.com (email works)
Please confirm if others have also faced this issue.