Skip to content

Instantly share code, notes, and snippets.

@FedericoPonzi
Created May 19, 2018 10:26
Show Gist options
  • Save FedericoPonzi/5bee0c3fd64ca546e070fe51eee0ff08 to your computer and use it in GitHub Desktop.
Save FedericoPonzi/5bee0c3fd64ca546e070fe51eee0ff08 to your computer and use it in GitHub Desktop.
An example to send HTML mail with attachment in java. No ssl/tls. Raw
package me.fponzi;
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 java.io.File;
import java.util.Date;
import java.util.Properties;
public class SendMail
{
private final Properties props;
private final Session session;
private MimeMultipart multipart;
private MimeMessage message;
public SendMail(String username, String password, String smtp, String port)
{
//Set the host smtp address
this.props = new Properties();
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
// create some properties and get the default Session
this.session = Session.getDefaultInstance(props, new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});
session.setDebug(false);
message = new MimeMessage(this.session);
multipart = new MimeMultipart();
}
public SendMail createMail(String recipients[], String from, String subject) throws MessagingException
{
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
message.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
message.setSubject(subject);
message.setSentDate(new Date());
return this;
}
public SendMail addHTMLMessage(String body) throws MessagingException
{
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body, "UTF-8", "html");
multipart.addBodyPart(messageBodyPart);
return this;
}
public SendMail addAttachment(File f, String filename) throws MessagingException
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(f);
messageBodyPart.setDisposition(Part.ATTACHMENT);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
return this;
}
public void send() throws MessagingException
{
// create a multipart
message.setContent(multipart);
Transport.send(message);
}
public static void main(String args[]) throws Exception
{
SendMail sendMail = new SendMail("username", "password", "mail.server.com", "port");
sendMail.createMail(new String[]{"[email protected]", "[email protected]"}, "[email protected]", "Hello, World!")
.addAttachment(new File("/home/fponzi/colosseo.jpg"), "Colosseo.jpg")
.addHTMLMessage("<strong>Hello, World!</strong>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment