sendmail.jsp: How do I know that mail was send ?
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 ?
E sad, mail posalje uspjesno ako se mail koji salje (from) navede ispravno.
Kako provjeriti da je "mail from" napisan ispravno, ili, kako provjeriti da
je mail poslan i prema tome napisati obavijest mail uspjesno poslan odnosno
nije poslan ?