-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
105 lines (74 loc) · 3.92 KB
/
Solution.java
File metadata and controls
105 lines (74 loc) · 3.92 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
//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)
package com.javarush.task.task40.task4003;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.Authenticator;
import java.util.Properties;
/*
Отправка письма с файлом
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
solution.sendMail("name.lastname@gmail.com", "password", "friend@gmail.com");
}
public void sendMail(final String username, final String password, final String recipients) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
setSubject(message, "Тестовое письмо");
setAttachment(message, "c:/text.txt");
Transport.send(message);
System.out.println("Письмо было отправлено.");
} catch (MessagingException e) {
System.out.println("Ошибка при отправке: " + e.toString());
}
}
public static void setSubject(Message message, String subject) throws MessagingException {
message.setSubject(subject);
}
public static void setAttachment(Message message, String filename) throws MessagingException {
// Create a multipar message
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
//Set File
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
//Add "file part" to multipart
multipart.addBodyPart(messageBodyPart);
//Set multipart to message
message.setContent(multipart);
}
}
/*
Отправка письма с файлом
Исправь реализацию метода setAttachment. Этот метод должен прикреплять файл к письму.
Подсказки:
1. Используй библиотеку JavaMail API версии 1.4.7.
2. Письмо должно содержать только одну часть (MimeBodyPart) с файлом.
Требования:
1. Метод setAttachment должен устанавливать новый контент у сообщения message. Тип контента должен быть MimeMultipart.
2. После вызова метода setAttachment, контент сообщения message должен содержать одну часть MimeBodyPart.
3. Метод setAttachment должен корректно устанавливать файл в соответствующий MimeBodyPart объект.
4. Метод setAttachment должен корректно устанавливать имя файла в соответствующий MimeBodyPart объект.
*/