Hiya vô địch. Giờ đây, chúng tôi đã có báo cáo JUnit ở định dạng PDF, hãy đính kèm báo cáo đó với email và gửi cho các bên liên quan khác nhau của dự án. Vì vậy, hôm nay chúng ta sẽ chủ yếu làm việc với Java. Lấy một tách cà phê [Java] tất cả các bạn
Chúng ta sẽ xem xét hai lớp
- Gửi thư. java – Lớp này có tất cả mã để gửi email
- gọi thư. java – Gọi SendMail. java bằng cách cung cấp từ địa chỉ, đến địa chỉ, dòng chủ đề và một số văn bản
Mã số
Gửi thư. java
package com.blog.utility;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/*
* This class has the main code for sending mail
*/
public class SendMail {
public static void send[String from, String tos[], String subject,
String text] throws MessagingException {
// Get the session 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"];
Session session = Session.getDefaultInstance[props,
new javax.mail.Authenticator[] {
protected PasswordAuthentication getPasswordAuthentication[] {
return new PasswordAuthentication[
"[email protected]",
"pass1234"];// change accordingly
}
}];
// compose message
try {
MimeMessage message = new MimeMessage[session];
message.setFrom[new InternetAddress[from]];// change accordingly
for [String to : tos] {
message.addRecipient[Message.RecipientType.TO,
new InternetAddress[to]];
}
/*
* for [String cc : ccs]
* message.addRecipient[Message.RecipientType.CC,new
* InternetAddress[cc]];
*/
message.setSubject[subject];
// Option 1: To send normal text message
// message.setText[text];
// Option 2: Send the actual HTML message, as big as you like
// message.setContent["This is actual message
" +
// text, "text/html"];
// Set the attachment path
String filename = "E:\\Selenium\\junit.pdf";
BodyPart objMessageBodyPart = new MimeBodyPart[];
// Option 3: Send text along with attachment
objMessageBodyPart.setContent[
"Mail from Selenium Project!
" + text, "text/html"];
Multipart multipart = new MimeMultipart[];
multipart.addBodyPart[objMessageBodyPart];
objMessageBodyPart = new MimeBodyPart[];
DataSource source = new FileDataSource[filename];
objMessageBodyPart.setDataHandler[new DataHandler[source]];
objMessageBodyPart.setFileName[filename];
multipart.addBodyPart[objMessageBodyPart];
message.setContent[multipart];
// send message
Transport.send[message];
System.out.println["message sent successfully"];
} catch [MessagingException e] {
throw new RuntimeException[e];
}
}// End of SEND method
}
gọi thư. java
package com.blog.junitTests;
import javax.mail.MessagingException;
import com.blog.utility.SendMail;
/*
* Invokes SendMail.java
*/
public class InvokeMail {
public static void main[String[] args] throws MessagingException {
//String to[] = {"[email protected]","[email protected]"};
String to[] = {"[email protected]"};
SendMail.send["[email protected]", to, "JUnit Report", "Check the PDF attachment."];
}
}
Giải trình
Nhìn trực tiếp vào mã khiến chúng ta cảm thấy khó hiểu. Hãy để chúng tôi hiểu một đoạn tại một thời điểm
Như mọi khi, bước đầu tiên của chúng tôi là tải xuống một vài JAR
- kích hoạt. cái lọ
- javax. thư-1. 6. 1. cái lọ
Tôi cũng đã đặt cả hai thứ này trong repo GitHub của chúng tôi, cùng với tất cả các tệp mã khác được xử lý như một phần của bài đăng này
Thêm các JAR này vào đường dẫn xây dựng dự án của chúng tôi. Chúng tôi đã thấy quy trình này nhiều lần trước đây và do đó tôi sẽ không lặp lại quy trình này [Tham khảo Bước 3 của bài viết này để được giải thích chi tiết]
Hiểu SendMail. Java,
1. Phương thức bên trong tất cả mã được viết để chúng ta có thể gọi nó dễ dàng từ bất kỳ lớp nào
public static void send[String from, String tos[], String subject,
String text] throws MessagingException {}
2. Các thuộc tính đã cho sẽ chỉ hoạt động cho Gmail. Trong trường hợp bạn đang sử dụng Outlook hoặc bất kỳ dịch vụ nào khác theo yêu cầu của dự án, thì những dịch vụ này sẽ được thay đổi cho phù hợp
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"];
3. Nhận đối tượng phiên và chuyển thông tin đăng nhập tài khoản email của bạn [đối với thông tin đăng nhập mà bạn đề cập từ địa chỉ]
Session session = Session.getDefaultInstance[props,
new javax.mail.Authenticator[] {
protected PasswordAuthentication getPasswordAuthentication[] {
return new PasswordAuthentication["[email protected]","pass1234"];// change accordingly
}
}];
4. Bây giờ là phần thú vị. Chúng tôi sẽ chỉ định địa chỉ “từ” và “đến”
message.setFrom[new InternetAddress[from]];
for [String to : tos] {
message.addRecipient[Message.RecipientType.TO,
new InternetAddress[to]];
}
Nếu bạn chỉ muốn gửi email này cho một người, thì hãy thay đổi mã như bên dưới,
message.setFrom[new InternetAddress[from]];
message.addRecipient[Message.RecipientType.TO,
new InternetAddress[to]];
Nếu bạn muốn gửi nó cho một số người bằng cc [bản sao], thì hãy thay đổi mã như bên dưới,
message.addRecipient[Message.RecipientType.CC,new InternetAddress[cc]];
5. Đặt dòng chủ đề là, ____1_______1
6. Để gửi,
- Một tin nhắn văn bản bình thường,
2package com.blog.junitTests; import javax.mail.MessagingException; import com.blog.utility.SendMail; /* * Invokes SendMail.java */ public class InvokeMail { public static void main[String[] args] throws MessagingException { //String to[] = {"[email protected]","[email protected]"}; String to[] = {"[email protected]"}; SendMail.send["[email protected]", to, "JUnit Report", "Check the PDF attachment."]; } }
- Thông báo HTML thực tế, kích thước tùy thích,
3package com.blog.junitTests; import javax.mail.MessagingException; import com.blog.utility.SendMail; /* * Invokes SendMail.java */ public class InvokeMail { public static void main[String[] args] throws MessagingException { //String to[] = {"[email protected]","[email protected]"}; String to[] = {"[email protected]"}; SendMail.send["[email protected]", to, "JUnit Report", "Check the PDF attachment."]; } }
- Văn bản và tệp đính kèm [đó là những gì chúng tôi đang tìm kiếm],
// Set the attachment path
String filename = "E:\\Selenium\\junit.pdf";
BodyPart objMessageBodyPart = new MimeBodyPart[];
objMessageBodyPart.setContent["Mail from Selenium Project!
" + text, "text/html"];
Multipart multipart = new MimeMultipart[];
multipart.addBodyPart[objMessageBodyPart];
objMessageBodyPart = new MimeBodyPart[];
DataSource source = new FileDataSource[filename];
objMessageBodyPart.setDataHandler[new DataHandler[source]];
objMessageBodyPart.setFileName[filename];
multipart.addBodyPart[objMessageBodyPart];
message.setContent[multipart];
7. Gửi email với một dòng đơn giản,
Transport.send[message];
Hiểu InvokeMail. Java,
Lớp này rất đơn giản để hiểu vì chúng ta chỉ gọi phương thức 'send' từ SendMail. java bằng cách cung cấp tất cả các đối số cần thiết
package com.blog.junitTests;
import javax.mail.MessagingException;
import com.blog.utility.SendMail;
/*
* Invokes SendMail.java
*/
public class InvokeMail {
public static void main[String[] args] throws MessagingException {
//String to[] = {"[email protected]","[email protected]"};
String to[] = {"[email protected]"};
SendMail.send["[email protected]", to, "JUnit Report", "Check the PDF attachment."];
}
}
0Khi “Run as -> Java application”, đầu ra của bảng điều khiển Eclipse IDE như bên dưới,
Email sẽ được nhận trong hộp thư đến của người nhận
Email đã tạo với tệp đính kèm được hiển thị để bạn tham khảo
ghi chú dính. coi chừng. Bạn có thể va vào “javax. thư. AuthenticationFailedException”. Ngoại lệ này chủ yếu xảy ra do các tính năng bảo mật và bảo vệ do Google cung cấp. Một cách giải quyết dễ dàng là BẬT quyền truy cập vào “Cho phép ứng dụng kém an toàn hơn” cho mục đích thử nghiệm bằng cách nhấp vào liên kết, “https. //www. Google. com/settings/security/lesssecureapps”