-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendEmailAtachament.java
More file actions
150 lines (106 loc) · 4.96 KB
/
Copy pathSendEmailAtachament.java
File metadata and controls
150 lines (106 loc) · 4.96 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.contentspeed.automationtesting.Lider.src.src;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class SendEmailAtachament {
public static void SendEmailPassed(String from, String to1, String subject, String filename) throws FileNotFoundException, IOException {
try {
//Get properties object
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.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, "Herculane#2019");
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to1));
//message.addRecipient(Message.RecipientType.BCC, new InternetAddress(grupSephora));
message.setSubject(subject);
// message.setText(msg);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Raport teste automate");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// messageBodyPart = new MimeBodyPart();
// DataSource source = new FileDataSource(filename);
// messageBodyPart.setDataHandler(new DataHandler(source));
// messageBodyPart.setFileName(filename);
// multipart.addBodyPart(messageBodyPart);
// message.setContent(multipart);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (Exception ex) {
System.out.println("eroare trimitere email-uri");
System.out.println(ex.getMessage());
}
} catch (Exception ex) {
System.out.println("eroare trimitere email-uri " + ex.getMessage());
}
}
public static void SendEmail(String from, String to1, String to2, String subject, String filename) throws FileNotFoundException, IOException {
try {
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(filename);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("rezultat TC-uri");
attachment.setName("rezultat TC-uri");
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("andacristea72@gmail.com", "cwwvzncxvmzsspat"));
email.setSSLOnConnect(true);
email.addBcc(to1);
email.addBcc(to2);
email.setFrom(from, "Teste Automate");
email.setSubject(subject);
email.setMsg(subject);
// add the attachment
//email.attach(attachment);
// StringWriter writer = new StringWriter();
// IOUtils.copy(new FileInputStream(new File(filename)), writer);
// email.setContent(writer.toString(), "text/html");
// email.attach(attachment);
// send the email
email.send();
writer.close();
} catch (Exception ex) {
System.out.println("eroare trimitere email-uri");
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
try {
System.out.println("=======send email start");
// nu a mers
SendEmail("anda72cristea@gmail.com", "andadeacu@yahoo.com", "", "Teste email-uri Gmail", "");
System.out.println("=======end email");
} catch (Exception ex) {
// ex.getMessage();
ex.printStackTrace();
}
}
}