message.setText(“Dear Recipient,” +”\n\n Hurray!! Mail is here.”);
Now, Send Message:
Transport.send(message);
NOTE:
If you are trying to send mail using gmail SMTP, first you need to disable security. You can find this option in your gmail setting or when you send mail, you will get one mail from gmail which will help you to disable security.
You can find below the code which sends mail using gmail SMTP:
package com.helical.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;
public class SendMailGmail {
public static void main(String[] args)
{
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 javax.mail.Authenticator(){
message.setText(“Dear Recipient,”+ “\n\n Mail has received”);
Transport.send(message);
System.out.println(“Mail Sent Successfully”);
}
catch (MessagingException e)
{
throw new RuntimeException(e);
}
}
}
Troubleshooting:
There are few exception, which you get often:
UnknownHostException : SMTP HOST
Ping to your SMTP host from command line. Make sure you get response (Success) because sometimes your firewall may block your connection.
javax.mail.AuthenticationFailedException
Check your username and password. If this is correct, try to login using your browser and make sure given user information are correct.
Could not connect to SMTP host
Check your SMTP host name and SMTP port, this is correct or not. Execute telnet command from your command prompt and check you are able to connect or not. Telnet command is:
C:\> telnet <SMTP HOST> <SMTP PORT>
Thanks
Sharad Sinha
Best Open Source Business Intelligence Software Helical Insight is Here
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
DisagreeAgree
I allow to create an account
When you login first time using a Social Login button, we collect your account public profile information shared by Social Login provider, based on your privacy settings. We also get your email address to automatically create an account for you in our website. Once your account is created, you'll be logged-in to this account.
For deeper insights, you may also explore: SSRS Reports end of life SSRS structured migration approach SQL Server Reporting Services (SSRS) has served enterprises well for traditional reporting needs. However, as Microsoft continues to evolve toward cloud-first and modern analytics...
For deeper insights, you may also explore: why SSRS Reports migration is required SSRS automation limitations Migrating from SQL Server Reporting Services (SSRS) to Pentaho is a strategic initiative for organizations seeking scalable, flexible, and open-source business intelligence. A successful...
For deeper insights, you may also explore: Crystal Reports vs Jaspersoft SSRS step-by-step migration approach Organizations modernizing their BI and reporting stacks often face a familiar challenge: migrating from SQL Server Reporting Services (SSRS) to more flexible, open-source platforms like...