I have created the below program as it is use to send the mail through java mail api and it is working perfectly now i have to modify it as i also want to attach a pdf file which is stored in my c: , please advise how can i modify this program below so that when it send the mail it should also send the abc.pdf file stored in my c: drive as an attachment also
c:\abc.pdf
below is the program which i have developed rite now it did not send any attachment
public class BrokMailTest {
public static void main(String[] args) {
String mailSmtpHost = "16.66.66.66";
String mailSmtpPort = "2345" ;
String mailTo = "[email protected]";
//String mailCc = "[email protected] ";
String mailFrom = "[email protected]";
String mailSubject = "fghfdwwrtuijjjjuj";
String mailText = "wertyuuiiojgfdss";
sendEmail(mailTo, mailFrom, mailSubject, mailText, mailSmtpHost ,mailSmtpPort );
}
public static void sendEmail(String to, String from, String subject, String text, String smtpHost , String mailSmtpPort) {
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mailSmtpPort", mailSmtpPort);
//obtaining the session
Session emailSession = Session.getDefaultInstance(properties);
Message emailMessage = new MimeMessage(emailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//emailMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse("[email protected],[email protected]"));
Address[] cc = new Address[] {
new InternetAddress("[email protected]"),
new InternetAddress("[email protected]")};
emailMessage.addRecipients(Message.RecipientType.CC, cc);
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailMessage.setContent(emailMessage, "text/html");
emailMessage.setText(text);
emailSession.setDebug(true);
Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}