Unable to send more than 5 emails to Exchange server using JavaMail - email

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

Related

How to run a cgi program as a google authorised user - as 3rd party apps are now forbidden [duplicate]

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.

Is there a work around google disabling "Less secure apps"?

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.

Xamarin, get error sending mail using SmtpClient

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.

Error empty content gmail using javamail api send email smtp

I'm working project : jsf, richfaces. I has just updated javamail api newest version 1.5.5 at https://java.net/projects/javamail/pages/Home
When i test send email from gmail to my gmail,
Subject : Subject test.
Content : Content test. And config : smtp.gmail.com, 465, SSL.
It has just subject and no content in my inbox receive gmail :
And log :
And my code :
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Set the host smtp address
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
.......
.......
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setText("UTF8");
// set the from and to address
InternetAddress addressFrom = new InternetAddress(SMTP_AUTH_USER);
msg.setFrom(addressFrom);
System.out.println(addressFrom);
msg.setSubject(subject, "utf-8");
......
......
try {
String content = replaceCharacterInEmail(cus, null,null, message);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(content, "text/html; charset=utf-8");
// attach the file to the message
Multipart mp = new MimeMultipart();
int count = 0;
if(files != null){
while (count < files.size()) {
MimeBodyPart mbdp = new MimeBodyPart();
mbdp.attachFile(files.get(count));
mp.addBodyPart(mbdp);
count++;
}
}
// create the Multipart and add its parts to it
mp.addBodyPart(mbp1);
// add the Multipart to the message
msg.setContent(mp);
Transport.send(msg);
} catch (Exception e) {
invalidAddress += (", " + address.trim());
e.printStackTrace();
}finally{
if (!"".equals(emailFails)) {
emailFails = emailFails.substring(1);
}
}
But, when i test above code at another project test (java application) with above lib javamail. It's ok :
.
I don't think problem at version of lib javamail. I don't know different between 2 project, because part send email is similar. How can i fix that error ?
Please observe this email structure:
-- multipart/mixed
-- mutlipart/alternative
-- text/plain
-- text/html
-- application/octet-stream (or any other mimetypes)
I found problem is conflict library at lib of jboss server and lib of app.ear (deploy).

Smtp Server Timeout error

I am trying to send mail using SmtpClient and below is my code.
SmtpClient client_ = new SmtpClient("relay-hosting.secureserver.net", 25);
//client_.DeliveryMethod = SmtpDeliveryMethod.Network;
//client_.EnableSsl = true;
// client_.UseDefaultCredentials = false;
//client_.Credentials = new System.Net.NetworkCredential(_fromAddress, _password);
MailAddress from_ = new MailAddress(_fromAddress, _fromName);
MailMessage msg_ = new MailMessage(from_, from_);
msg_.Subject = "Subject";
StringBuilder body_ = new StringBuilder();
body_.AppendLine("Line1");
body_.AppendLine("===============================================================================================");
body_.AppendLine("Line2");
body_.AppendLine("===============================================================================================");
body_.AppendLine("line2");
body_.AppendLine("===============================================================================================");
msg_.Body = body_.ToString();
msg_.IsBodyHtml = true;
client_.Send(msg_);
I am getting TimeOut error. When Same smtp configuration using in email client on my machine, it send the mail immediately. I don't know what can be cause. Also when I used my Gmail account with gmail smpt server it worked.
Most consumer ISPs block port 25 to prevent you from running a mail server.
Therefore, you cannot connect to port 25 from your house.
You can ask them for an alternate port.
Try port 587; it's commonly used as well.