Sending Email Using Gmail & Java
Tuesday, 11 February 2014Posted by
Amrishwa
0 Comments
TO Send Email Using java we have to get java mail api you can get it here
Java Mail Api There after import Api to library.
on jButton1ActionPerformed write down the code below
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
final String username = jTextField1.getText();
final String password = jTextField2.getText();
final String reciepent=jTextField3.getText();
final String subject=jTextField4.getText();
final String msg=jTextArea1.getText();
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() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(reciepent));
message.setSubject(subject);
message.setText(msg);Transport.send(message);
jLabel5.setText("done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Subscribe to:
Post Comments (Atom)