Desperatly trying to send an email to myself:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.web.de");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress("myveryownemail#web.de"));
msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("myveryownemail#web.de")});
msg.setSubject("whatever");
msg.setContent("whatever", "text/plain");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.web.de", 587, "myveryownemail", "myveryownandcorrectpassword");
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
I´m getting
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
but username and password is absolutly correct (tried username with and without the #web.de). password is the one I use for normal login into my mail account.
don´t get it
Create the javax.mail.Session object like below, with your username and
password:-
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Ref tutorial:- http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
In past I solved similar issue adding this:
if ("true".equals(emailConfig.get("mail.smtp.auth"))) {
session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailConfig.get("mail.smtp.user"), emailConfig.get("mail.smtp.password"));
}
});
}
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(emailFromAddress));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailToAdrress));
msg.setSubject(mailSubject);
msg.setText(mailMessageBody);
Transport.send(msg);
Session is javax.mail.Session
Set the below properties with your smtp user and password
props.put("mail.smtp.user", "myuser");
props.put("mail.smtp.password", "mypwd");
Reference : http://www.tutorialspoint.com/java/java_sending_email.htm
Hope you have included the "mail.jar" and "activation.jar" in your project. This link may help you
You're calling the Transport.send static method, which ignores the Transport instance you created and connected. Call the sendMessage method instead, or skip creating your own Transport instance and call the static send method that takes a username and password.
This is one of the JavaMail common mistakes.
Related
I am trying to send mail using Java code . The code is working fine when running on my personal PC . But on office network the exception of unknown SMTP host is appearing. Also my office pc is not able to ping smtp.gmail.com. PC firewall is closed as well.
Is there any other way to establish the connection? I am also providing my code below for reference.
mport javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;
public class otp {
String d_email = "email#gmail.com",
d_password = "password",
d_uname="uname",//your email password
d_host = "mail.outlook.com",
d_port = "587",
m_to = "target#gmail.com", // Target email address
m_subject = "Testing Mail programs",
m_text = "Hey, this is a test email.";
public otp() {
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
System.out.println(1);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
System.out.println(3);
Transport transport = session.getTransport("smtp");
transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
Transport.send(msg);
System.out.println("Message Sent succesfully");
} catch (Exception mex) {
mex.printStackTrace();
}
}
public static void main(String[] args) {
otp blah = new otp();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}
}
I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.
for the granular analyses,please attach your code.
you are using a gmail account but you have provided a outlook host , try the following template:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailUsingGMailSMTP {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "xyz#gmail.com";//change accordingly
// Sender's email ID needs to be mentioned
String from = "abc#gmail.com";//change accordingly
final String username = "abc";//change accordingly
final String password = "*****";//change accordingly
// >> gmail host
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject("Testing Subject");
// Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}enter code here
}
}
I am wondering why the below basic java mail program is not working because I didn't get any errors as the program is executed just fine. Is there anything that I am doing wrong? Any help would be appreciated.I would also like to add that I also tried it using wrong username and password combination but still getting no error and the programs runs completely fine.
public class emailfromgmail {
public static void main(String[]args)
{
final String from = "username";
final String pass = "password";
String to = "recipient#gmail.com";
String host="smtp.gmail.com";
String subject = "java Mail";
String body = "example of java mail api using gmail smtp";
//get the session object
Properties p = System.getProperties();
p.put("mail.smtp.starttls.enable","true");
p.put("mail.smtp.host",host);
p.put("mail.smtp.user",from);
p.put("mail.smtp.password",pass );
p.put("mail.smtp.port", "587");
p.put("mail.smtp.auth","true");
Session session = Session.getInstance(p,
new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject(subject);
msg.setText(body);
Transport.send(msg);
System.out.print("message sent successfully");
}
catch(MessagingException e){
e.printStackTrace();
}
}
}
I had the same problem - I ran my program and put in a dialogue box in various places to see where it stopped doing anything. I still don't know what was wrong, but I tried a totally different approach and it worked. Instead of sending via TLS, I send via SSL. I used this website:
https://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/
Their TLS didn't work - it just didn't do anything when I ran it. Their SSL worked like a charm!
Here is the code:
package com.mkyong.common;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailSSL {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from#no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("to#no-spam.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
i am creating a simple java mail program,the program is working ok and the last system print also working .but the problem is i dint received the mail in outlook.here i am using the company outlook.please some one help me.
i am attaching my code here
enter code here
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleSendEmail
{
public static void main(String[] args)
{
String host = "compny host";
String from = "mail id";
String to = "usr#some.com";
String subject = "birthday mail";
String messageText = "I am sending a message using the"
+ " simple.\n" + "happy birthday.";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("compny host", host);
props.put("mail.smtp.port", "25");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props, null);
// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
session.setDebug(sessionDebug);
try
{
Message msg = new MimeMessage(session);
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);
Transport.send(msg);
System.out.println("Sent message successfully....");
}
catch (MessagingException mex)
{
mex.printStackTrace();
}
}
}
output
Sent message successfully....
"Compny host" doesn't seem like correct host. Check out this tutorial http://www.tutorialspoint.com/java/java_sending_email.htm and here you have also a few examples of sending emails in Java Send email using java
I do expect that you are using the correct host on your side.
But you are missing Username and Password.
transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);
transport.sendMessage(message, message.getAllRecipients());
or you can use the Authenticator:
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
This is my code. I am getting the following exception.
final String username = "mymail#gmail.com";
final String password = "mypass";
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");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("fazeen.ahmad93#gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("fazeenahmad1993#gmail.com"));
message.setSubject("Testing subject");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("test body");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
message.setContent(multipart);
Transport.send(message);
Exception I am getting:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. pl7sm1333988wic.4 - gsmtp
at Test.main(Test.java:200)
Caused by: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. pl7sm1333988wic.4 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1020)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:716)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:388)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at Test.main(Test.java:195)
I think you are missing to add to your properties the following fuse..
mail.smtp.starttls.required=true
Use this running code for sending mail
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author xyz
*/
public class MailSend {
public static void main (String [] args){
String to="xyz#gmail.com";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email_address","password");//change accordingly
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("email_address"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Welcome!!");
message.setText("Hiii buddy ");
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
if you have no smtp server on your machine then download it and run after downloading.
try this
I was Using old version of mail.jar use this jar mail-1.4.7.jar and after that for authentication To open Account Access : https://www.google.com/settings/security/lesssecureapps (turn on) this was the problem if using google smtp
This program attempts to send email by first connecting to smtp.rediffmail.com . There is no compile time error or compile time exception.But as i try to run the following program it generates the following exception.
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by
doing get message first
I can't figure out what the exception is and why i am getting this exception .
Here is the complete program.In this i have tried to make TLS connection with rediffmail server.
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
class rediff {
public static void main(String args[]) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.rediffmail.com");
props.put("mail.stmp.user", "from");
//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "password");
Session session = Session.getDefaultInstance(props, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "from";
String password = "password";
return new PasswordAuthentication("from", "password");
}
});
String to = "me#gmail.com";
String from = "from#rediff.com";
String subject = "Testing...";
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText("rediff program working...!");
Transport transport = session.getTransport("smtp");
transport.send(msg);
System.out.println("fine!!");
} catch(Exception exc) {
System.out.println(exc);
}
}
}
Why do i get this exception ?
as per: http://www.techtalkz.com/microsoft-outlook/193842-pop3.html
In your account settings, enable the "Log on to incoming server before
sending mail" on the "Outgoing Server" tab of your account properties. How
to locate these properties and tabs is Outlook version-specific but you
decided that information wasn't important.
The error is specific to the SMTP service you are trying to use from your client. It's not a code problem. Check your rediffmail.com account settings
The problem with the code is this:
Transport transport = session.getTransport("smtp");
transport.send(msg);
the send method is a static method and you are not suppose to access it with an instance of Transport class. it should be - Transport.send(msg);
You're trying to use TLS auth but I don't see any port settings in your code. Usually smtp server uses different ports for TLS/SSL authentication, try setting it via mail.smtp.socketFactory.port. For TLS default value is 587, for SSL - 993 as far as I remember.
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");