import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.mail.Session;
import javax.mail.Transport;
public
class
SendEmail
{
public
static
void
main(String [] args)
{
String host =
"127.0.0.1"
;
Properties properties = System.getProperties();
properties.setProperty(
"mail.smtp.host"
, host);
Session session = Session.getDefaultInstance(properties);
try
{
MimeMessage message =
new
MimeMessage(session);
message.setFrom(
new
InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO,
new
InternetAddress(recipient));
message.setSubject(
"This is Subject"
);
BodyPart messageBodyPart1 =
new
MimeBodyPart();
messageBodyPart1.setText(
"This is body of the mail"
);
BodyPart messageBodyPart2 =
new
MimeBodyPart();
String filename =
"attachment.txt"
DataSource source =
new
FileDataSource(filename);
messageBodyPart2.setDataHandler(
new
DataHandler(source));
messageBodyPart2.setFileName(filename);
Multipart multipartObject =
new
MimeMultipart();
multipartObject.addBodyPart(messageBodyPart1);
multipartObject.addBodyPart(messageBodyPart2);
message.setContent(multipartObject);
Transport.send(message);
System.out.println(
"Mail successfully sent"
);
}
catch
(MessagingException mex)
{
mex.printStackTrace();
}
}
}