First Commit from Source Code Reply

This commit is contained in:
vincenzofariello
2024-05-09 17:40:24 +02:00
parent 11e3b57c5b
commit 107a016cb9
35225 changed files with 1111346 additions and 1 deletions

View File

@@ -0,0 +1,128 @@
package it.valueteam.gnp.test_svil;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.util.Properties;
public class SendMail {
Properties properties;
Authenticator auth;
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator(String username, String password) {
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
public SendMail(String serverMail, String serverMailPort, String user, String password)
{
this.properties = new Properties();
properties.put("mail.smtp.host", serverMail);
properties.put("mail.user", user);
properties.put("mail.password", password);
if(user != null && !"".equals(user)) {
properties.put("mail.smtp.auth", "true");
auth = new Authenticator(user, password);
}
else properties.put("mail.smtp.auth", "false");
properties.put("mail.smtp.port", serverMailPort);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.debug", "true");
}
public void sendEmail(String to, String from, String subject, String text) throws Exception {
try {
Session emailSession = Session.getInstance(this.properties, this.auth);
//Session emailSession = Session.getDefaultInstance(this.properties, null); // oppure this.auth
Message emailMessage = new MimeMessage(emailSession);
if (to != null && to.split(",").length > 1) {
String[] destinations = to.split(",");
for(int i=0; i < destinations.length ; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(destinations[i]));
}
} else {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailMessage.setText(text);
emailSession.setDebug(true);
Transport transport = emailSession.getTransport("smtp");
transport.connect((String) properties.get("mail.smtp.host"), (String) properties.get("mail.user"), (String) properties.get("mail.password"));
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
//Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
throw e;
} catch (MessagingException e) {
e.printStackTrace();
throw e;
}
}
public void sendEmailWithAttachment(String to, String from, String subject, String text, File fileToAttach, String attachFilename) throws Exception {
try {
Session emailSession = Session.getInstance(this.properties, this.auth);
Message emailMessage = new MimeMessage(emailSession);
if (to != null && to.split(",").length > 1) {
String[] destinations = to.split(",");
for(int i=0; i < destinations.length ; i++) {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(destinations[i]));
}
} else {
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailSession.setDebug(true);
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(text);
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
if (fileToAttach != null) {
try {
DataSource source = new FileDataSource( fileToAttach );
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachFilename);
multipart.addBodyPart(messageBodyPart);
} catch (Exception e) {
e.printStackTrace();
}
}
// Send the complete message parts
emailMessage.setContent(multipart);
Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
throw e;
} catch (MessagingException e) {
e.printStackTrace();
throw e;
}
}
}