I have a simple interface that looks like below:
It's pretty self-explanatory and I can enter my recipient email and it will pass the value to a function.
My javascript function(when send button invoked):
<script>
//function to insert content of email to table
function sendmessage(){
var recipient = document.getElementById("recipient").value;
var subject = document.getElementById("subject").value;
var content=document.getElementById("content").value;
$.ajax({
url: 'sendemail.jsp',
type: 'POST',
data: {
recipient:recipient,
subject:subject,
content:content
},
success: function (data) {
alert("Successfully send email");
},
error: function (request, error) {
alert("Request: " + JSON.stringify(error));
}
});
}
</script>
<body>
To:<input type="text" style="font-size: 10pt;" size="30" id="recipient" ><br><br>
Subject:<input type="text" style="font-size: 10pt" size="70" id="subject" ><br><br>
Content:<br><textarea cols="100" rows="10" id="content" style="font-size: 13pt;">
<%=files%>: <%=url%>
</textarea><br>
<div class="Send">
<button type="button" onclick="sendmessage()"> Send </button>
</div>
It passes to my sendemail.jsp(snippet):
<%
String recipient=request.getParameter("recipient");
String subject=request.getParameter("subject");
String content=request.getParameter("content");
fileFacade.sendEmail(recipient,subject,content);
%>
And my email function:
public void sendEmail(String recipient,String subject,String content) {
try {
final String fromEmail = "[email protected]"; //requires valid gmail id
final String password = "xxxx"; // correct password for gmail id
System.out.println("Please Wait, sending email...");
/*Setup mail server */
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.port", "25"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(content);
try {
Transport.send(message);
} catch (AddressException addressException) {
addressException.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
My code above works for a single recipient and now I would like to allow it to send to multiple recipient like in the format of shown in the screenshot above. I do not want to create a CC column for reasons.
How do I pass multiple values of emails to multiple recipients in the way I want?
You don't need to use "split". Just use:
message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));