All of the fields must be filled.
You cannot swap a section with a page.
You must first select a group before adding a user to it.

Sending an email in Java is actually quite simple, as always, there is an API that will do most of the work for you and it becomes just a matter of implementation. You will also require access to an SMTP server in order to send the mail.
First of all, if you don't have the JavaMail API, then download it. Add the jar to your classpath or your lib directory.
Before we do anything, ideally, you should create a new class which handles the method of sending emails for your application. You'll also need your SMTP server information, but for simplicity, we will use 127.0.0.1 in this example.
In order to set the SMTP server, you must add it to Java's System Properties, this is done by simply calling System.getProperties() and adding to the properties the key mail.smtp.host with the value being your SMTP server ip.
Afterwards all there is left to do is building the message. You'll see that on the javax.mail.Message there are quite a few methods which you can explore depending on what you want to do (ie. adding a bcc) but for this example, we'll simply set the sender, the recepient, the subject and the content (supporting html content).
| public static void send(String subject, String recepient, String sender, String content) throws Exception { //Set the properties and build the necessary objects.
final Properties props = System.getProperties();
props.put("mail.smtp.host", "127.0.0.1");
final Session session = Session.getDefaultInstance(props);
final Message message = new MimeMessage(session);
//You can change the encoding here.
final String CHARSET = "UTF-8";
message.setSubject(MimeUtility.encodeText(subject, CHARSET, null));
//Build the addresses and set them on the message.
final InternetAddress add = new InternetAddress(sender);
add.setPersonal(MimeUtility.encodeText(sender, CHARSET, null));
message.setFrom(add);
message.setSentDate(new Date());
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recepient));
final MimeMultipart htmlMultipart = new MimeMultipart("related");
//Add the HTML supported content to the email's body.
final BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html");
messageBodyPart.addHeader("charset", CHARSET);
messageBodyPart.addHeader("Content-Transfer-Encoding", "quoted-printable");
//Package the email and send it.
htmlMultipart.addBodyPart(messageBodyPart);
message.setContent(htmlMultipart);
Transport.send(message);
} |
All you have to do in order to have all the imports is to include javax.mail.* or if you're using an IDE, a fix import will take care of it for you if you've included the JavaMail jar file in your project's library. Feel free to copy and paste the code above and modify it to your needs.