package com.org.sbollam;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;
public class SendMailExample {
public static void main(String[] args) {
String msg = " Mail sent successfully (java mail service application)";
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.debug", "true"); // Debug the program
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("*****@gmail.com","*****@***");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("*****@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("****@gmail.com"));
message.setSubject(" **SAI HAI- ** ");
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//message body
messageBodyPart.setText("**************** SAY Hi TO, How R U *************** ");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("C:\\Users\\demo\\Desktop\\hai.txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("hai.txt");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
//System.out.println("Email sent successfully using this java application");
} catch (MessagingException e) {
msg = "Error while sending";
throw new RuntimeException(e);
}
JOptionPane.showMessageDialog(null, msg);
}
}
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;
public class SendMailExample {
public static void main(String[] args) {
String msg = " Mail sent successfully (java mail service application)";
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.debug", "true"); // Debug the program
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("*****@gmail.com","*****@***");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("*****@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("****@gmail.com"));
message.setSubject(" **SAI HAI- ** ");
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
//message body
messageBodyPart.setText("**************** SAY Hi TO, How R U *************** ");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("C:\\Users\\demo\\Desktop\\hai.txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("hai.txt");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
//System.out.println("Email sent successfully using this java application");
} catch (MessagingException e) {
msg = "Error while sending";
throw new RuntimeException(e);
}
JOptionPane.showMessageDialog(null, msg);
}
}