Created
July 27, 2011 10:06
-
-
Save timendum/1109059 to your computer and use it in GitHub Desktop.
Sending a Mail via JavaMail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.util.List; | |
import java.util.Properties; | |
import javax.activation.DataHandler; | |
import javax.activation.DataSource; | |
import javax.activation.FileDataSource; | |
import javax.mail.Address; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.Session; | |
import javax.mail.Transport; | |
import javax.mail.internet.AddressException; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMessage; | |
import javax.mail.internet.MimeMultipart; | |
public class Mail { | |
public static final String PASSWORD = "mail.password"; | |
public static final String USER = "mail.user"; | |
public static final String SMTP_HOST = "mail.smtp.host"; | |
public static final String TRANSPORT_PROTOCOL = "mail.transport.protocol"; | |
public static final String AUTH = "mail.smtp.auth"; | |
public void send(String from, String to, String cc, String bcc, String subject, String body, List<File> attachments) throws MessagingException, java.io.IOException { | |
Properties prop = new Properties(); | |
prop.load(new FileInputStream(new File("mail.properties"))); | |
Session session = Session.getDefaultInstance(prop); | |
MimeMessage msg = new MimeMessage(session); | |
msg.setFrom(new InternetAddress(from)); | |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); | |
msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc)); | |
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); | |
msg.setSubject(subject); | |
if (attachments == null || attachments.size() == 0) { | |
msg.setText(body); | |
} else { | |
Multipart multipart = new MimeMultipart(); | |
MimeBodyPart messageBodyPart = new MimeBodyPart(); | |
messageBodyPart.setText(body); | |
multipart.addBodyPart(messageBodyPart); | |
for (File file : attachments) { | |
messageBodyPart = new MimeBodyPart(); | |
DataSource source = new FileDataSource(file); | |
messageBodyPart.setDataHandler(new DataHandler(source)); | |
messageBodyPart.setFileName(file.getName()); | |
multipart.addBodyPart(messageBodyPart); | |
} | |
msg.setContent(multipart); | |
} | |
msg.saveChanges(); | |
if (Boolean.parseBoolean(prop.getProperty(AUTH))) { | |
Transport transport = session.getTransport(prop.getProperty(TRANSPORT_PROTOCOL)); | |
transport.connect(prop.getProperty(SMTP_HOST), prop.getProperty(USER), prop.getProperty(PASSWORD)); | |
transport.sendMessage(msg, msg.getAllRecipients()); | |
transport.close(); | |
} else { | |
Transport.send(msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment