Jump To Right Section
Show
In this article, we want to discuss the way to Send email in java through Gmail Server. We can send emails by using the SMTP server of Gmail. It is good if you are don’t have any SMTP server and reliable. Here we will learn how to send email through the Gmail server by SSL (Secure Sockets Layer). SSL is basically used for security if you are sending email through a Gmail server.
For a better understanding of this example, learn the steps of sending an email using JavaMail API first. For sending the email using JavaMail API, you need to load the two jar files:
- mail.jar
- activation.jar
download these jar files (or) go to the Oracle site to download the latest version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public void sendMail() throws MessagingException{ String host = "smtp.gmail.com"; String password = "abc123"; String toAddress = enterEmail.getText().toString(); Properties properties = System.getProperties(); properties.put("mail.smtp.host", host); properties.put("mail.smtps.auth", true); properties.put("mail.smtp.starttls.enable", true); Session session = Session.getInstance(properties, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, toAddress); message.setSubject("Anti-Theft Attachment"); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Your email address is saved as backup email in Anti-Theft Application"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); try{ Transport transport = session.getTransport("smtps"); transport.connect(host, from, password); transport.sendMessage(message, message.getAllRecipients()); System.out.println("Mail Sent Successfully"); transport.close(); } catch (SendFailedException sfe){ System.out.println(sfe); } } |
Example of Sending Email through Gmail Server with SSL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; class Mailer{ public static void send(String from,String password,String to,String sub,String msg){ //Get properties object Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); //get Session Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from,password); } }); //compose message try { MimeMessage message = new MimeMessage(session); message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); message.setSubject(sub); message.setText(msg); //send message Transport.send(message); System.out.println("message sent successfully"); } catch (MessagingException e) {throw new RuntimeException(e);} } } public class SendMailSSL{ public static void main(String[] args) { //from,password,to,subject,message //change from, password and to } } |
After reading this article we think, now you can send email in java. You may like Variables and Data Types in Java.
Send Email in Java through Gmail Server
The article was published on October 31, 2016 @ 12:44 PM
Leave a Comment