I am able to send mails using SMTP. But I need to implement Oauth to avoid using password while sending mails. This can be done using accesstoken.
After lot of surfing, i got below snippet.
Properties props = new Properties();
props.put("mail.smtp.user", userEmail);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = "";
transport.connect(host, port, userEmail, emptyPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
I am struggling to connect using SMTPTransport.connect with empty password.
Can anyone help me whether I can SEND_MAILS + SMTP + OAUTH + GMAIL_ACCESS_TOKEN?
I appreciate working code suggestions. Thanks
Related
Following is my code to send email from the corporate MS office email id, but I am getting the error - javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful. My username and password are correct.
final String user="abc#abc.com";
final String password = "1234";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
//props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(sendEmailAddress));
String text = "Test email";
message.setSubject("Test email");
message.setText(text);
Transport.send(message);
Try this:
props.put("mail.smtp.host", "smtp.office365.com");
Reference: Outlook mail settings
I can't send an e-mail with java code via Gmail.
CODE:
import java.util.Properties;
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;
public class b {
public static void sendMessage(String to, String from, String subject, String text){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(text);
Transport transport = session.getTransport("smtp");
String mfrom = "fromemail";
transport.connect("smtp.gmail.com", mfrom, "fromemailpassword");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
I get the error:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. b3sm24500496wiw.22 - gsmtp
I am trying to send an e-mail from a Gmail account to a Gmail accout without success. I need to do this for my business e-mail to send out newsletters ect.
EDIT:
I changed a section of code to this:
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true"); //make it true
properties.put("mail.smtp.starttls.enable", "true"); //make it true
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
//Authentication is needed use your gmail user and password"smtp.gmail.com"
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email#gmail.com", "password");
}
};
Session session = Session.getInstance(properties, auth);
And now I get the exception:
javax.mail.AuthenticationFailedException
I think that the problem is that your properties are contradictory.
You are setting the SMTP port to 587, which (according to this page) is the server port for SMTP over TLS. But then you are setting mail.smtp.starttls.enable to false.
Naturally the Google SMTP server is confused ... and it tells you that it was expecting the client to send a STARTTLS command.
Solutions:
Change the mail.smtp.starttls.enable property to true.
Alternatively, change the mail.smtp.port property to 465 (the SSL port)
You don't have the option of using "vanilla" SMTP ... which is a good thing if you care about securing your email traffic.
According to this resource, you also need to enable authentication. (The page has extensive Java example code ...)
The response code 530 means access denied
The accompanying message indicates that gmail is expecting a STARTTLS.
Try with
props.put("mail.smtp.starttls.enable", "true");
Although it may need more properties to work properly
Besides: non-secure SMTP is always a bad idea unless its local.
starttls should be true.
means add :
props.put("mail.smtp.starttls.enable", "true");
then post me that working or not ?
Do it like this
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true"); //make it true
properties.put("mail.smtp.starttls.enable", "true"); //make it true
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
//Authentication is needed use your gmail user and password"smtp.gmail.com"
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourGmailUserName", "yourGmailpassword");
}
};
Session session = Session.getInstance(properties, auth);
I am building an android app. I want to send an email from xxxxx#outlook.com. This is the code.
public void setUp
{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
this.mailhost = "smtp.live.com";
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.port", "587");
props.setProperty("mail.smtp.starttls.enable", "true");
}
I know that user should be the whole email address. But when I used I received an email that said I should start session before send a email.
This code worked 3 times and then stopped.
What does the debug output show? What was the exact error message? Exactly what did it say in the email you received? You may need to connect with POP3 or IMAP to read mail before it will let you send mail.
You should get a session from Javamail, to create your message. Then, using a transport object, you can send it.
String host = "localhost";
int port = 443;
String user = "BruceWayne#example.org";
String password = "S3cr3tP4ss";
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user, "Dark Knight"));
msg.setSubject("Hello Selina");
msg.setText("Do you want to have diner ?");
Transport transport = session.getTransport("smtp");
transport.connect(host, port, user, password);
transport.sendMessage(msg, msg.getAllRecipients());
I try to send mail using below code but I am getting Exception like.
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)
at mail.MailTest.sendMailSSL(MailTest.java:116)
Please any one help me how can I solve it.
Here is my code.Thanks
String mailBODY = "<h3>Hi this is my test Mail</h3>;
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");
Session session = Session.getInstance(props, new GMailAuthenticator(username, password));
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("TEST"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(test#gmail.com));
message.setSubject("Mail subject test");
message.setContent(mailBODY,"text/html; charset=utf-8");
Transport.send(message);
System.out.println("mail has been send");
} catch (MessagingException e) {
e.printStackTrace();
}
Try adding props and sending like this:
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
// ...
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
// ...
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
I got this error after I enabled 2-step verification on my google account
I've got gmail and yahoo working, but not hotmail. Here's what I have, what am I doing wrong?
private String mailhost = "smtp.live.com";
public hotmailSenderActivity(String user, String password) {
this.user = user;
this.password = password;
//This connects to the actual mailserver
Security.addProvider(new com.provider.JSSEProvider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
I have tried port 25 + 587 without the SSL stuff. I have tried port 465 WITH the SSL stuff. The email and password are correct (Ive hard coded them to be sure).
I don't receive any errors... So whats the problem?
1) use debug output:
session.setDebug(true);
2) hotmail smtp server starts non-ssl connection on port 25 or 587, and uses starttls after initial connection; thus remove lines
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
3) mimimum amount of settings is then:
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
this assumes port is 25, otherwise add props.put("mail.smtp.port", "587");
4) yet even nicer looks this:
...
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
Transport trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 25, "user", "pass");
now you're connected, use methods of Transport
http://www.oracle.com/technetwork/java/javamail/faq/index.html#hotmail
Get rid of all that socket factory stuff, you don't need it.