Link to home
Start Free TrialLog in
Avatar of integrosys
integrosys

asked on

Send e-mail with attachment

I have the following method that uses Java Mail to send an e-mail. How do I put an attachment in the e-mail?

public static void sendEmail(String recipient, String subject, String body, String sender, String organisation, String attachment) {
  Properties props = new Properties();
  props.put("mail.smtp.host", "172.22.3.48");
  props.put("mail.smtp.connectiontimeout", "45000");
  props.put("mail.smtp.timeout", "44550");
  Session session = Session.getDefaultInstance(props, null);
  session.setDebug(false);
  try {
    MimeMessage msg = new MimeMessage(session);
    msg.setHeader("Content-Type", "text/plain");
    msg.saveChanges();
    msg.setFrom(new InternetAddress(sender));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
    msg.setSubject(subject);
    msg.setText(body);
    msg.setSentDate(new Date());
    Transport.send(msg);
  } catch (Exception exception) {
    ...
  }
}
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial