I have below code to send an email to my gmail.
public class Mainclass {
public static void main(String args[]){
String host = "smtp.gmail.com";
String from = "from_address#fdsl.com";
String to = "mymail#gmail.com";
// Set properties
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", true);
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("username", "mymail#gmail.com");
props.put("password", "mypassword");
// Get session
Session session = Session.getInstance(props);
try {
// Instantiate a message
Message msg = new MimeMessage(session);
// Set the FROM message
msg.setFrom(new InternetAddress(from));
// The recipients can be more than one so we use an array but you can
// use 'new InternetAddress(to)' for only one address.
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// Set the message subject and date we sent it.
msg.setSubject("Email from JavaMail test");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is the text for this simple demo using JavaMail.");
// Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
But when i run the code i always get below the exception.
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
at javax.mail.Service.connect(Service.java:329)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at Mainclass.main(Mainclass.java:66)
Am i missing anything here? My password is correct. Please help me.
Thanks!
Have you set the host correctly ? Also, I don't see which ports are you using to connect. Have you set the correct ports? Is TLS the correct auth encryption for gmail ?
Please see the attached config:
'SMTP::TLS',
Host => 'smtp.gmail.com',
Port => 587,
User => 'username#gmail.com',
Password => 'password'
Related
I'm trying to send email using java API and i'm giving the right emailid and password but still i get AuthenticationFailedException.
I also tried giving host=mail.smtp.port and changing port to 587 still i end up getting the same error..
Please help me where i'm going wrong..?
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "to#gmail.com";
// Sender's email ID needs to be mentioned
final String from = "from#gmail.com";
// Assuming you are sending email from localhost
final String host = "smtp.googlemail.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.user", from);
//properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.debug", "false");
properties.setProperty("mail.smtp.auth", "true");
// properties.setProperty("mail.smtp.port", "587");
// Get the default Session object.
// Session session = Session.getDefaultInstance(properties);
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, "xyz");
}
});
session.setDebug(true);
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);
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
Error:
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
Just check if you have enabled logging in from "Less Secure Apps" using this link. This setting needs to be enabled for the account from#gmail.com.
i have desktop application and i face this error when sending mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection refused: connect
~code~
String host="smtp.mail.yahoo.com";
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host",host);
props.put("mail.smtp.host", host);
props.put("mail.stmp.user", "abc#yahoo.ca");//User name
//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "mypassword"); //Password
props.put("mail.smtp.socketFactory.fallback", "false");
Session session1 = Session.getDefaultInstance(props, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "abc#yahoo.ca";
String password = "mypassword";
return new PasswordAuthentication(username, password);
}
});
MimeMessage msg = new MimeMessage(session1);
String from = "abc#yahoo.ca";
String subject = "Testing...";
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
msg.setSubject(subject);
Transport.send(msg);
YahooMail smtp port is 465 or 587
Add this :-
props.put("mail.smtp.port", "465");
or
props.put("mail.smtp.port", "587");
Did you find the JavaMail FAQ?
These entries will help:
How do I access Gmail with JavaMail?
How do I debug problems connecting to my mail server?
What are some of the most common mistakes people make when using JavaMail?
Try this:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "abcd#gmail.com";
// Sender's email ID needs to be mentioned
String from = "web#gmail.com";
// 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();
}
}
}
I have a MailSender class that has 2 methods:
private Session createSmtpSession(final String fromEmail, final String fromEmailPassword) {
final Properties props = new Properties();
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", "" + 587);
props.setProperty("mail.smtp.starttls.enable", "true");
props.setProperty ("mail.transport.protocol", "smtp");
return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, fromEmailPassword);
}
});
}
public void sendEmail(String subject, String fromEmail, String fromEmailPassword, String content, String toEmail){
Session mailSession = createSmtpSession(fromEmail, fromEmailPassword);
mailSession.setDebug (true);
try {
Transport transport = mailSession.getTransport ();
MimeMessage message = new MimeMessage (mailSession);
message.setSubject (subject);
message.setFrom (new InternetAddress (fromEmail));
message.setContent (content, "text/html");
message.addRecipient (Message.RecipientType.TO, new InternetAddress (toEmail));
transport.connect ();
transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO));
}
catch (MessagingException e) {
System.err.println("Cannot Send email");
e.printStackTrace();
}
}
ok, The way the above code works is like this:
I have personal gmail myEmail#gmail.com & if I want to send msg to destination email like aa#xyz.com from myEmail#gmail.com, then I need to call sendEmail(subject, "myEmail#gmail.com", fromEmailPassword, content, "aa#xyz.com");
But recently I bought VPS from Godaddy & I am using Godaddy Email (info#mydomain.com) so I want to send smg to destination email like aa#xyz.com from info#mydomain.com, then I need to call sendEmail(subject, "info#mydomain.com", fromEmailPassword, content, "aa#xyz.com");
However I got error cos Godaddy using port 25
So I changed the code in createSmtpSession as following:
props.setProperty ("mail.host", "smtp.mydomain.com");
props.setProperty("mail.smtp.port", "" + 25);
However, this time I got other problem
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 OK
DEBUG SMTP: MessagingException while sending, THROW:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <aa#xyz.com> Recipient not found. <http://x.co/irbounce>
Clearly the aa#xyz.com is actually existed, but why the error saying Recipient not found.
SO How to fix this issue?
I need to look at the outgoing (SMTP) server (like smtpout.secureserver.net) in Godaddy Email & now it working fine
props.setProperty ("mail.transport.protocol", "smtps");
props.setProperty ("mail.smtps.host", "smtpout.secureserver.net");
props.setProperty("mail.smtps.auth", "true");
props.setProperty("mail.smtps.port", "" + 465);
I am sending email using
public void sendEmail(String fromEmailAddr, String toEmailAddr,String subject, String emailBody) {
String host = "xxx";
final String user = "user";
final String password = "password";
// Get system properties
Properties properties = new Properties();
// Setup mail server
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", "25");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties, null);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmailAddr));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmailAddr));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(emailBody);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
When i try to send email using above code then it comes to message Sent message successfully.... but i got no email. On the other hand if i use authentication then i got the email
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
Why ? Is it necessary to provide userName and password for host ? Can i send email by just specifying host, no username and password provided ?
Thanks
I guess the port number might be the issue.
Try changing properties.put("mail.smtp.port", "25");
to properties.put("mail.smtp.port", "587");.
Further you can refer this.
It depends on the mail server you're using.
For example, some mail servers will let you send mail to anyone in the same company without authentication, but authentication is needed to send mail outside of the company. In the latter case, if you send the mail without authenticating, the mail server may accept the message and return a "mailer-daemon" failure message, or might just throw the message away.
Also, see this list of common JavaMail mistakes.
I am writing a simple Java program to send mail, but am getting errors. Here's the code:
package mypackage;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Send a simple, single part, text/plain e-mail
public class Sendmail {
public static void main(String[] args) {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "atul.krbhatia#gmail.com";
String from = "atul.krbhatia#gmail.com";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "smtp.gmail.com";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiatee a message
Message msg = new MimeMessage(session);
System.out.println("in try blk");
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("This is a test of sending a " +
"plain text e-mail through Java.\n" +
"Here is line 2.");
//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
System.out.println("in catch block");
mex.printStackTrace();
}
}
}//End of class
Here's the errors:
221 2.0.0 closing connection d1sm3094152pbj.24
in catch block
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. d1sm3094152pbj.24
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at mypackage.Sendmail.main(Sendmail.java:48)
Gmail only supports SMTP over SSL/TLS.
Add
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
You also need to login to the server:
props.put("mail.smtp.host", "smtp.gmail.com");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
A simple google search for "javamail gmail" will yield many example of how to use JavaMail with GMail, like this one. Google also has a configuration page listing the connection configuration that you'll need so you can double check the settings.
This URL May help:
http://sbtourist.blogspot.com/2007/10/javamail-and-gmail-its-all-about.html
It looks like the server is expecting SSL. There are several other questions on here where people are also trying to send mail through Gmail using Java, I recommend taking a look at them.
How can I send an email by Java application using GMail, Yahoo, or Hotmail?
Must issue a STARTTLS command first. Sending email with Java and Google Apps