java: Sending failed - java

After multiple researches I can't succeed to send any mail with java
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("matr#live.fr"));
InternetAddress toAddress = new InternetAddress("matrphone#gmail.com");
msg.addRecipient(Message.RecipientType.TO, toAddress);
msg.setSubject("sujet du mail de text");
msg.setText("aaa");
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com", 587,"matr#live.fr", "mypasswordcached");
transport.send(msg);
I have Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtps
I don't understand why
Anyone can help me please ?

Try the following code:
package org.kodejava.example.mail;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class GmailSendEmailTLS {
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static void main(String[] args) throws Exception {
//
// Email information such as from, to, subject and contents.
//
String mailFrom = "email#gmail.com";
String mailTo = "email#gmail.com";
String mailSubject = "TLS - Gmail Send Email Demo";
String mailText = "TLS - Gmail Send Email Demo";
GmailSendEmailTLS gmail = new GmailSendEmailTLS();
gmail.sendMail(mailFrom, mailTo, mailSubject, mailText);
}
private void sendMail(String mailFrom, String mailTo,
String mailSubject, String mailText)
throws Exception {
Properties config = createConfiguration();
//
// Creates a mail session. We need to supply username and
// password for Gmail authentication.
//
Session session = Session.getInstance(config, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
GmailSendEmailTLS.USERNAME,
GmailSendEmailTLS.PASSWORD
);
}
});
//
// Creates email message
//
Message message = new MimeMessage(session);
message.setSentDate(new Date());
message.setFrom(new InternetAddress(mailFrom));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(mailSubject);
message.setText(mailText);
//
// Send a message
//
Transport.send(message);
}
private Properties createConfiguration() {
return new Properties() {{
put("mail.smtp.auth", "true");
put("mail.smtp.host", "smtp.gmail.com");
put("mail.smtp.port", "587");
put("mail.smtp.starttls.enable", "true");
}};
}
}
This example taken from: Sending email using gmail via TLS

Related

JavaxException Unknown SMTP host Exception: mail.gmail.com

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

How to send Java email using smtp.mail.yahoo.com?

I am trying to send emails from "smtp.mail.yahoo.com". I have tried using different port numbers like 587, 465 etc., It works fine when I send using gmail host. But when i try using yahoo, I am getting the below exception.
package com.java.sample.workouts;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class JavaSendEmail {
private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
private static final String SMTP_AUTH_USER = "myyahooid#yahoo.com";
private static final String SMTP_AUTH_PWD = "mypassword";
public static void main(String[] args) throws Exception{
new JavaSendEmail().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.ssl","true");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
}
});
Transport transport = session.getTransport("smtp");
MimeMessage message = new MimeMessage(session);
message.setContent("This is a test", "text/plain");
message.setFrom(new InternetAddress("myyahooid#yahoo.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("myyahooid#yahoo.com"));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
But I am getting the below exception
Exception in thread "main" javax.mail.AuthenticationFailedException: 535 5.7.1 Authentication failed
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at com.java.sample.workouts.JavaSendEmail.test(JavaSendEmail.java:46)
at com.java.sample.workouts.JavaSendEmail.main(JavaSendEmail.java:18)
Follow these steps to enable Less Secure Apps in your Yahoo account:
Go to Account Info
Select “Account Security”
Enable “Allow Apps that use less secure
sign in”
I just tested your code and it's working

How to send simple Email using Javamail?

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);
}
});

Error when sending mail in Java

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();
}
}
}

How i send email without authentication using java

I would like to send email without authentication using java.
Can someone help me?
With authentication, I do it as follows:
public void sendEmail() throws EmailException{
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.addTo("XXX#gmail.com", "XXXX");
email.setFrom("XXXX#gmail.com","XXXXX");
email.setSubject("testando . . .");
email.setMsg("testando 1");
email.setSSL(true);
email.setAuthentication("xxxxxx#gmail.com", "XXXXX");
email.send();
}
I forgot to say that i do not have a provider. i need an provider finally, i have emailFrom Subject and Message, and need send this email how?
If it is only for testing purposes, you may try Papercut. While it’s running, Papercut automatically picks up e-mail sent to the standard SMTP port (25) on any IP address. You just send mail from your application and switch to Papercut to review it.
Papercut # github:
https://github.com/ChangemakerStudios/Papercut/releases
import java.util.Date;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "127.0.0.1");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("admin#test.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("a#b.com"));
message.setSubject("Notification");
message.setText("Successful!", "UTF-8"); // as "text/plain"
message.setSentDate(new Date());
Transport.send(message);
}
}
import java.util.*;
import javax.mail.*;
public class SimpleEmail {
public static void main(String[] args) {
System.out.println("SimpleEmail Start");
String smtpHostServer = "smtp.gmail.com";
String toEmail = "XXXX#gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHostServer);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(props);
try {
MimeMessage msg = new MimeMessage(session);
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("n91eply#gmail.com", "NoReply-JD"));
msg.setReplyTo(InternetAddress.parse("n91eply#gmail.com", false));
msg.setSubject("SimpleEmail Testing Subject", "UTF-8");
msg.setText("SimpleEmail Testing 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();
}
}
}
Try this example:
// File Name SendEmail.java
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();
}
}
}
Via: tutorialspoint

Categories