How to send email from java? .For this we need an JavaMail API .Include this jar file in your class path by placing it into YourProject\WebContent\WEB-INF\lib or if you are not using EE version then add it into environment variable.
First we need to login to mail account.
- Email server consist two types of port, one is for incoming mail(POP, IMAP) and another is for outgoing mail(SMTP).For sending purpose we only need SMTP port of that particular Email server.
- Second thing is how to send authentication data to server using insecure network.To maintain security there is two protocol SSL and TLS which send authentication data(Email,Password) to server in encrypted form,If we use TLS ,it allow both secure and unsecure connection,and if SSL it only allow secure Connection.That is up to user to choose the protocol
- In java to carry Email account setting,we use Properties,These Email settings are
2. smtp port (either for protocol SSL or for protocol TLS)
3 valid EmailId and password of active account
- And for Authentication we use javax.mail.Authenticator() and save it into session while getting the Default instance of Properties and Authenticator().
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); / / Host Name
props.put("mail.smtp.socketFactory.port", "465");/ / SSL Port
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{ protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
}
});
props.put("mail.smtp.host", "smtp.gmail.com"); / / Host Name
props.put("mail.smtp.socketFactory.port", "465");/ / SSL Port
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{ protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
}
});
Now Code for Login in Gmail using TLS protocol
Actually Gmail use STARTTLS
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");/ / Host Name
props.put("mail.smtp.port", "587"); / / TLS Port
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{ protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
}
});
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");/ / Host Name
props.put("mail.smtp.port", "587"); / / TLS Port
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
{ protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("YOUREMAIL@GMAIL.COM", "PASSWORD");
}
});
- Now after login we have to compose email,First understand our email message cannot simply transfer through http,for this a particular standard are set and those are mime standard,our email server use mime to transfer email ,first it convert the email message into mime format and transfer it on network Know more about Mime
- Email Header contain all the information about the message and Routing information.
- Use MimeMessage to create mail format. It provide option to set From ,where and To whom ,email is going to be sent and it also provide setSubject to set the Subject of Email.
- After that,it give freedom to setContext of email.Use text/html if you want to send html in message else there is no need to setContext to text/html
- At Last Transport.send(message),same as we click on send button in email client application
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("YOUREMAIL@GMAIL.COM"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(TO@XYZ.COM));
message.setSubject("My First Email using Behind Java Scene");
String msg="Thank's to Behind Java Scene to teach the core aspect of Email sending "
message.setContent(msg, "text/html; charset=utf-8");
Transport.send(message);
message.setFrom(new InternetAddress("YOUREMAIL@GMAIL.COM"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(TO@XYZ.COM));
message.setSubject("My First Email using Behind Java Scene");
String msg="Thank's to Behind Java Scene to teach the core aspect of Email sending "
message.setContent(msg, "text/html; charset=utf-8");
Transport.send(message);
Email Server Settings for Yahoo
Only change the host Name and SMTP port from Login code
Host Name :smtp.mail.yahoo.com
SMTP port: 465
Properties props = new Properties();
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.auth", true);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.host", "smtp.mail.yahoo.com");
props.put("mail.smtp.auth", true);
Email Server Settings for Hotmail
Only change the host Name and SMTP port from Login code in SSL section
Host Name: smtp.live.com
SMTP port: 25
Host Name: smtp.live.com
SMTP port: 25
Same for Hosting Server only change your particular Host Name and change your specific SMTP port
hotmail Not working
Use port 587 instead of 25 and everything should work just fine
Thank you for the information, I think this article is very useful for all who read it.
.
I really enjoyed reading this post, big fan. Keep up the good work andplease tell me when can you publish more articles or where can I read more on the subject? Hotmail Support
It's true Bluehost is the industry leading hosting provide and they are best For wordpress Hosting,
BlueHost Basic Plan Discount – $3.49/mo* for first contract
No coupon or promotion code needed – Just purchase via our promo link (see below) and you’ll get an extra 55% discount from your first BlueHost bill.
BlueHost WEb Hosting Discount
awesome post and Yahoo is one of the top most search engines, other than this, it is also well known for its mail services. Yahoo has various strong featured services
which are mostly liked & used by the yahoo users worldwide.if you want to deeply knowledge about the Yahoo mail so you have to visit my site and also contact to the
Yahoo customer support team.
This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information...
msn hotmail sign in login
Please share more like that.
hotmail.com hotmail account
Nice article! I loved it.
Fix hotmail sign in problem
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
Devops Training courses
python Training in chennai
Devops Training in Bangalore
Best Devops Training in pune
I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium interview questions and answers
I am really impressed with your efforts and really pleased to visit this post.
python Training institute in Chennai
python Training institute in Bangalore
python Training in Pune
This comment has been removed by the author.
Such a great information for blogger iam a professional blogger thanks…
Softgen Infotech is the Best SAP GRC Training in Bangalore located in BTM Layout, Bangalore providing quality training with Realtime Trainers and 100% Job Assistance.
slot siteleri
kralbet
betpark
tipobet
betmatik
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
LNC3Q
شركة مكافحة بق الفراش بالدمام tOgp8fKPE4