So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.
This is the error I'm getting:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp
I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account
Less secure apps & your Google Account
You could try authentification via "App password".
On your Google account:
set 2-Step Verification ON
create 16-character "App password"(
How to create app password) -> result should be similar to:
16-character "App password"
Instead of Google account password use 16-character password
MailMessage mail = new MailMessage();
foreach (string receiver in DolociPrejemnike())
mail.To.Add(receiver);
mail.From = new MailAddress("app_gmail#gmail.com", "No replay"); //poĊĦiljatelj (vedno enak)
mail.Subject = SetSubject();
mail.Body = SetBody();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("app_gmail#gmail.com", "xtqapucsmyvqmvxp"); // Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
So thanks for all the replays! i have fixed this issue by doing this:
I have enabled the "App password for windows machines"
Then i simply changed the password from the email password to the google generated one
and changed the code to the following:
public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
String myAccountEmail="yourEmailAddress#gmail.com";
String password="Generated Windows machine password from google";
Session session=Session.getInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message=prepareMessage(session,myAccountEmail,recepient,order);
Transport.send(message);
System.out.println("Message Sent successfully");
}
private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
message.setSubject("Your subject here");
message.setText(");
return message;
} catch (AddressException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2
I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);
Apps password
option two is to go to your google account and generate an apps password
When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.
To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.
So in the following code:
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Simply change it to the following:
props.put("mail.imap.auth.mechanisms", "XOAUTH");
and it should work, keeping all else the same.
Note: please enable 2-factor authentication in google account before proceeding.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.
Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords
Use 16 digit code provided by google instead of password and that will serve as authentication token.
Related
So since the 31 of may google has disabled the option for "Less secure apps", so I have been using the Java mail API, and since the update i can no longer send emails using the Gmail smtp.
This is the error I'm getting:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp
I switched to an outlook mail and it seems to work fine, but i was wondering if there is a way to use a Gmail account
Less secure apps & your Google Account
You could try authentification via "App password".
On your Google account:
set 2-Step Verification ON
create 16-character "App password"(
How to create app password) -> result should be similar to:
16-character "App password"
Instead of Google account password use 16-character password
MailMessage mail = new MailMessage();
foreach (string receiver in DolociPrejemnike())
mail.To.Add(receiver);
mail.From = new MailAddress("app_gmail#gmail.com", "No replay"); //poĊĦiljatelj (vedno enak)
mail.Subject = SetSubject();
mail.Body = SetBody();
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("app_gmail#gmail.com", "xtqapucsmyvqmvxp"); // Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
So thanks for all the replays! i have fixed this issue by doing this:
I have enabled the "App password for windows machines"
Then i simply changed the password from the email password to the google generated one
and changed the code to the following:
public class JavaMailUtil {
public static void sendMail(String recepient,String order) throws Exception {
Properties properties=new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
String myAccountEmail="yourEmailAddress#gmail.com";
String password="Generated Windows machine password from google";
Session session=Session.getInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myAccountEmail, password);
}
});
Message message=prepareMessage(session,myAccountEmail,recepient,order);
Transport.send(message);
System.out.println("Message Sent successfully");
}
private static Message prepareMessage(Session session,String from,String to,String orderInfo) {
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));a
message.setSubject("Your subject here");
message.setText(");
return message;
} catch (AddressException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Now that you can no longer use login and password with Googles smtp server the only option really is to use XOauth2
I havent used Jakarta before but it appears to support it. You should look into OAuth2 Support
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("imap.gmail.com", username, oauth2_access_token);
Apps password
option two is to go to your google account and generate an apps password
When running your code use the password generated instead of the actual users password. The main issue with this being there is no telling how long google will continue to support apps password.
To those who followed the other answers but are still getting the "Authentication Failed" error when using an app password, a key point is that this solution is NOT working for XOAUTH2 if you are using that or are following a guide saying to use oauth2.
So in the following code:
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
Simply change it to the following:
props.put("mail.imap.auth.mechanisms", "XOAUTH");
and it should work, keeping all else the same.
Note: please enable 2-factor authentication in google account before proceeding.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) options is no longer available.
Instead use another way of using apppasswords provided by google.
https://myaccount.google.com/u/0/apppasswords
Use 16 digit code provided by google instead of password and that will serve as authentication token.
I am using System.Net.Mail.SmtpClient to send mail in my Xamarin Form app. It's set to using my gmail address, and working great.
I would like to get the error from the smtp server (if there is one) to inform the user.
So, I am using the event _SendCompleted
Here my code
(sending email)
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(outils.emailEmetteur);
mail.To.Add("toto#outlook.frfr");//Tiers.contactEmail);
mail.Subject = outils.emailObjetDefaut;
mail.Body = outils.emailMessageDefaut;
bool fileExist = File.Exists(filePath);
if (fileExist)
{
Attachment pJ = new Attachment(filePath);
mail.Attachments.Add(pJ);
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new NetworkCredential(outils.emailEmetteur, outils.emailEmetteurMDP);
try
{
smtpServer.SendAsync(mail, null);
smtpServer.SendCompleted += smtpServer_SendCompleted;
return true;
}
catch (Exception ex)
{
}
}
(send completed event)
private static void smtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
string token = (string)e.UserState;
string info = "";
if (e.Cancelled)
{
info = "Send canceled.";
}
if (e.Error != null)
{
info = e.Error.ToString();
}
else
{
info = "Message sent.";
}
}
I am trying to send an email to an incorrect address (toto#outlook.frfr)
My event is correctly triggered, but e.Cancelled and e.Error are NULL, and, in my Gmail inbox, I receive an error from smtp server telling me that the email address was incorrect, and that what I want to get in my Xamarin app.
Do you have any idea ? Thanks :)
Your client relays it to Gmail's outgoing mail server; the transaction to submit the message to SMTP is successful. The error only happens later, when Gmail tries to connect to the recipient's mail server, and can't resolve it; at that point, your client is no longer necessarily connected, and so the mail server generates a bounce message and delivers it to the sender's inbox.
In the general case, you either have to write your own mail server (but no, don't go there) or examine the inbox for bounce messages.
I'm using EmailService in order to send a basic email:
String subject = request.getParameter("subject");
String body = request.getParameter("body");
EmailAddressModel fromAddress = modelService.create(EmailAddressModel.class);
fromAddress.setDisplayName("test#test.es");
fromAddress.setEmailAddress("test#test.es");
List<EmailAddressModel> addresses = new ArrayList<>();
addresses.add(fromAddress);
EmailMessageModel email = modelService.create(EmailMessageModel.class);
email.setSubject(subject);
email.setBody(body);
email.setToAddresses(addresses);
email.setFromAddress(fromAddress);
email.setReplyToAddress("myaccount#gmail.com");
modelService.save(email);
emailService.send(email);
Where do I configure the password of "myaccount#gmail.com" in order to make the Hybris SMTP server authentificate with my personal mail and send the email?
You can configure below parameters in local.properties file or you can change them in hac for runtime only.
mail.smtp.server
mail.smtp.port
mail.smtp.user
mail.smtp.password
mail.pop3.beforesmtp
mail.pop3.password
mail.pop3.server
mail.pop3.user
mail.from
mail.replyto
mail.use.tls
You can get more detail from blog post.
XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
.setXmppDomain("404.city").setUsernameAndPassword("user", "password")
.setCompressionEnabled(false)
.setSecurityMode(ConnectionConfiguration.SecurityMode.required)
.setCustomSSLContext(sslcont)
.build();
XMPPTCPConnection connection = new XMPPTCPConnection(conf);
if(connection.isConnected()){
System.out.println("already connected");
return;
}
connection.connect();
connection.login();
I am new to smack, I am trying to use smack 4.2.1 to connect the xmppserver(openfire).The code is login with a username and password,but when I try to modify the code.There is no loginAnonymous function.
How Can I Login as a anonymous?Please guide me
There are two functions allowEmptyOrNullUsernames() and performSasAnonymousAuthentication().When set the configuration of XMPPTCPConnection ,it can be called.
I have a method that sends many emails to exchange server over smtp using JavaMail, below is my code,
public void sendMail(){
final String host="host",port="587",username="mail1#local.local",password="password",from="";
try {
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", port);
props.put("mail.smtp.ssl.trust", host);
final String email = from;
Authenticator authenticator = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
};
Session session = Session.getInstance(props,authenticator);
InternetAddress replyToAddress [] = new InternetAddress[1];
replyToAddress[0] = new InternetAddress("mail1#local.local");
Transport transport = session.getTransport("smtp");
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("mail1#local.local"));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("mail2#local.local"));
mimeMessage.setSubject("Test");
mimeMessage.setText("Hello Testing");
mimeMessage.setReplyTo(replyToAddress);
transport.send(mimeMessage);
System.out.println("Email has been Sent Successfully to");
} catch (MessagingException e) {
e.printStackTrace();
}
Now when I apply a loop and call this function 10 times then only first five emails are sent successfully, for rest of the request I get the following exception,
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Connection closed by remote host
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2157)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2144)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1210)
at javax.mail.Transport.send0(Transport.java:197)
at javax.mail.Transport.send(Transport.java:124)
if I increase the request count then still only first five emails are sent, rest of them throws the exception.
If I put the failed request in some queue and retry them later then some of them will be sent.
Any clue will be appreciated, do I need to check for some configuration on Exchange server?
We have seen this exact same issue with a MS Exchange Server 2010. We had to increase the ReceiveConnector message rate limit by issuing the following Powershell-command:
Set-ReceiveConnector "Client CLIENTNAME" -MessageRateLimit unlimited