· 10 years ago · May 06, 2016, 02:00 PM
1public void sendmail(){
2 System.out.println("started function");
3 String from = "amritharajeevan.77@gmail.com";
4 // Sender's email ID needs to be mentioned
5 String to = "amritha2.rajeevan@aricent.com";
6
7 final String username = "amritharajeevan.77";//change accordingly
8 final String password = "vivekrajeevan";//change accordingly
9
10 // Assuming you are sending email through gmail.com
11 String host = "smtp.gmail.com";
12
13 Properties props = new Properties();
14 props.put("mail.smtp.auth", "true");
15 props.put("mail.smtp.starttls.enable", "true");
16 props.put("mail.smtp.host", host);
17 props.put("mail.smtp.port", "25");
18 Session session = Session.getInstance(props,
19 new javax.mail.Authenticator() {
20 protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
21 return new javax.mail.PasswordAuthentication(username, password);
22 }
23 });
24
25 try {
26 Message message = new MimeMessage(session);
27 message.setFrom(new InternetAddress(from));
28 message.setRecipients(Message.RecipientType.TO,
29 InternetAddress.parse(to));
30 message.setSubject("Server Error");
31 BodyPart messageBodyPart = new MimeBodyPart();
32 messageBodyPart.setText("Server"+" " +servererror+ " " +" is unreachable. Please check the logs.");
33 Multipart multipart = new MimeMultipart();
34 multipart.addBodyPart(messageBodyPart);
35 messageBodyPart = new MimeBodyPart();
36 String filename = "/Users/aricent/Desktop/amritha/seetest/May6.zip";
37 DataSource source = new FileDataSource(filename);
38 messageBodyPart.setDataHandler(new DataHandler(source));
39 messageBodyPart.setFileName(filename);
40 multipart.addBodyPart(messageBodyPart);
41 message.setContent(multipart);
42 System.out.println("sent start function");
43 Transport.send(message);
44
45 System.out.println("Sent message successfully....");
46
47 } catch (MessagingException e) {
48 throw new RuntimeException(e);
49 }
50 }