-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendAttachmentInEmail.java
73 lines (61 loc) · 2.48 KB
/
SendAttachmentInEmail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
public class SendAttachmentInEmail {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
// String to="";//change accordingly
String to="[email protected]";
//Get the session object
Properties props = System.getProperties();
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.smtp.auth", "true");
props.put("mail.smtp.port", "465");
final String pass;
final String msg;
System.out.println("enter your password : ");
password = scan.nextLine();
System.out.println("Enter Your message : ");
msg=scan.nextLine();
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]",password);//change accordingly
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("attached file sent from java code:: ");
MimeBodyPart part2 = new MimeBodyPart();
String attachFile = "D:/FileName.xls"; //write your file name with an extension
DataSource source = new FileDataSource(attachFile);
part2.setDataHandler(new DataHandler(source));
part2.setFileName(new File(attachFile).getName());
BodyPart _msg = new MimeBodyPart();
_msg.setText(msg);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part2);
multipart.addBodyPart(_msg);
message.setContent(multipart);
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}