Sendmail Help
Hi, another newbie - my code is almost right, but I cannot work out how to send duplicate emails. I get a java.lang.ArrayIndexOutOfBoundsException, I have had the whole system working for one email, just not lots. Please help me, I am sure I am nearly there, I just need a final shove..
thanks
phil
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmail extends Helper
{
//Variables to pull in form details
public static String strChangeRequestNumber;
public static Object objChangeRequestType;
public static String strChangeDescription;
public static Object objRecipients;
public static String[] a;
//main procedure
public static void main(String[] args) throws IOException
{
ChangeRequestForm ChangeRequestForm = new ChangeRequestForm(null);
//get variables from Form when control is returned
String strChangeRequestNumber = ChangeRequestForm.strChangeRequestNumber;
String strRecipients = ChangeRequestForm.strRecipients;
Object objChangeRequestType = ChangeRequestForm.objChangeType;
String strChangeDescription = ChangeRequestForm.strChangeDescription;
objRecipients = ChangeRequestForm.objRecipient;
a = ChangeRequestForm.a;
Properties props = (Properties)System.getProperties().clone();
props.put("mail.smtp.host", "smtp.mysmtp.here");
// set as properties as needed using current session
Session session = Session.getInstance(props, null);
session.setDebug(true);
try {
Message msg = new MimeMessage(session);
//***edit in to dictate from address (if allowed)***
msg.setFrom(new InternetAddress("MyFromEmailAddressHere"));
//InternetAddress[] address = {new InternetAddress((String) objRecipients)};
//InternetAddress[] address = {new InternetAddress((String) strRecipients)};
InternetAddress[] addressTo = new InternetAddress[strRecipients.length()];
//(StrRecipients is produced from a combibox with multiple selections enabled, I am happy with the output from this - each email addr is seperated by a comma etc)
//For each recipient, send an email..if only...
for (int i = 0; i < strRecipients.length(); i++)
addressTo = new InternetAddress(a);
msg.setRecipients(Message.RecipientType.TO, addressTo);
///
//msg.setRecipients(Message.RecipientType.TO, address);
//msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("My subject here: " + objChangeRequestType + strChangeRequestNumber);
msg.setSentDate(new Date());
//HTML format of email
strMsgText =("<html><head><title></title></head>" +
"<span style = \"FONT-family: Tahoma\">" +
"<body><p>Hi,</p>" +
"<p>some text:<br><br>" +
"<span style=\"BACKGROUND-COLOR: #ffffff\">" +
objChangeRequestType +
strChangeRequestNumber +
"<br><br>" + "Description: " +
strChangeDescription +
"</span></p>" +
"<p> </p></body></html>");
msg.setContent(Helper.strMsgText, "text/html");
calculateDate();
writeEmailToFile();
Transport.send(msg);
displayMsgBox("email sent to" + ChangeRequestForm.a[ChangeRequestForm.q]);
displayMsgBox("email sent, no option to recall!");
}
catch (MessagingException e) {
displayMsgBox("error sending message");
e.printStackTrace();
}
}
}