forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
package siprp.medicina.processo.mail;
|
|
|
|
import java.util.Date;
|
|
import java.util.Properties;
|
|
|
|
import javax.mail.Address;
|
|
import javax.mail.Authenticator;
|
|
import javax.mail.Message;
|
|
import javax.mail.PasswordAuthentication;
|
|
import javax.mail.Session;
|
|
import javax.mail.Transport;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
public class MailSender
|
|
{
|
|
protected String from = "";
|
|
protected String mailServer = "mail2.evolute.pt";
|
|
protected String userName = "";
|
|
protected String password = "";
|
|
|
|
public MailSender()
|
|
{
|
|
|
|
}
|
|
|
|
public void send( String to, String bcc, String subject, String body,
|
|
String attachmentNames[], byte attachments[][] )
|
|
throws Exception
|
|
{
|
|
Properties props = System.getProperties();
|
|
props.put( "mail.smtp.host", mailServer );
|
|
props.put( "mail.smtp.auth", "true" );
|
|
|
|
Authenticator auth = new SMTPAuthenticator();
|
|
Session session = Session.getInstance( props, auth );
|
|
|
|
Message msg = new MimeMessage( session );
|
|
|
|
msg.setFrom( new InternetAddress( from ) );
|
|
msg.setReplyTo( new Address[]{ new InternetAddress(from)} );
|
|
msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to, true ) );
|
|
msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, true ) );
|
|
msg.setSubject( subject );
|
|
msg.setContent( body, "text/html" );
|
|
|
|
msg.setHeader( "X-Mailer", "Evolute Mailer" );
|
|
msg.setSentDate( new Date() );
|
|
Transport.send( msg );
|
|
}
|
|
|
|
private class SMTPAuthenticator extends javax.mail.Authenticator
|
|
{
|
|
|
|
public PasswordAuthentication getPasswordAuthentication()
|
|
{
|
|
return new PasswordAuthentication(userName, password);
|
|
}
|
|
}
|
|
}
|