Skip to content

Commit 8215072

Browse files
authored
to the woman were given two wings of a great eagle
And to the woman were given two wings of a great eagle, that she might fly into the wilderness, into her place, where she is nourished for a time, and times, and half a time, from the face of the serpent (Revelation 12:14)
1 parent 14436cd commit 8215072

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
2+
//And to the woman were given two wings of a great eagle, that she might fly into the wilderness,
3+
//into her place, where she is nourished for a time, and times, and half a time, from the face of the serpent (Revelation 12:14)
4+
5+
package com.javarush.task.task40.task4003;
6+
7+
import javax.activation.DataHandler;
8+
import javax.activation.DataSource;
9+
import javax.activation.FileDataSource;
10+
import javax.mail.*;
11+
import javax.mail.internet.InternetAddress;
12+
import javax.mail.internet.MimeBodyPart;
13+
import javax.mail.internet.MimeMessage;
14+
import javax.mail.internet.MimeMultipart;
15+
import javax.mail.Authenticator;
16+
import java.util.Properties;
17+
18+
/*
19+
Отправка письма с файлом
20+
*/
21+
22+
public class Solution {
23+
24+
public static void main(String[] args) {
25+
Solution solution = new Solution();
26+
solution.sendMail("[email protected]", "password", "[email protected]");
27+
}
28+
29+
public void sendMail(final String username, final String password, final String recipients) {
30+
Properties props = new Properties();
31+
props.put("mail.smtp.auth", "true");
32+
props.put("mail.smtp.starttls.enable", "true");
33+
props.put("mail.smtp.host", "smtp.gmail.com");
34+
props.put("mail.smtp.port", "587");
35+
36+
Session session = Session.getInstance(props,
37+
new Authenticator() {
38+
protected PasswordAuthentication getPasswordAuthentication() {
39+
return new PasswordAuthentication(username, password);
40+
}
41+
});
42+
43+
try {
44+
Message message = new MimeMessage(session);
45+
message.setFrom(new InternetAddress(username));
46+
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
47+
48+
setSubject(message, "Тестовое письмо");
49+
setAttachment(message, "c:/text.txt");
50+
51+
Transport.send(message);
52+
System.out.println("Письмо было отправлено.");
53+
54+
} catch (MessagingException e) {
55+
System.out.println("Ошибка при отправке: " + e.toString());
56+
}
57+
}
58+
59+
public static void setSubject(Message message, String subject) throws MessagingException {
60+
message.setSubject(subject);
61+
}
62+
63+
public static void setAttachment(Message message, String filename) throws MessagingException {
64+
// Create a multipar message
65+
Multipart multipart = new MimeMultipart();
66+
BodyPart messageBodyPart = new MimeBodyPart();
67+
68+
//Set File
69+
DataSource source = new FileDataSource(filename);
70+
messageBodyPart.setDataHandler(new DataHandler(source));
71+
messageBodyPart.setFileName(filename);
72+
73+
//Add "file part" to multipart
74+
multipart.addBodyPart(messageBodyPart);
75+
76+
//Set multipart to message
77+
message.setContent(multipart);
78+
}
79+
}
80+
81+
/*
82+
Отправка письма с файлом
83+
84+
Исправь реализацию метода setAttachment. Этот метод должен прикреплять файл к письму.
85+
86+
Подсказки:
87+
88+
1. Используй библиотеку JavaMail API версии 1.4.7.
89+
90+
2. Письмо должно содержать только одну часть (MimeBodyPart) с файлом.
91+
92+
93+
94+
95+
96+
Требования:
97+
98+
1. Метод setAttachment должен устанавливать новый контент у сообщения message. Тип контента должен быть MimeMultipart.
99+
100+
2. После вызова метода setAttachment, контент сообщения message должен содержать одну часть MimeBodyPart.
101+
102+
3. Метод setAttachment должен корректно устанавливать файл в соответствующий MimeBodyPart объект.
103+
104+
4. Метод setAttachment должен корректно устанавливать имя файла в соответствующий MimeBodyPart объект.
105+
*/

0 commit comments

Comments
 (0)