I have written below a program to send an mail through java mail api which send the mail now my query is to handle the exceptional scenarios also lets say if mail is not sent then i have to da something and if mail is send i have to do some other thing in that cas , now please advise does java mail api proved us any parameter while sending the mail buy which we can check that mail is been sent successfully or not as i have enabled the debugging in my program
emailSession.setDebug(true);
please advise which is the parameter in return sent by java mail api by which we can check mail is sent successfully or not
below is my simple program of java mail api
public class EmailTest {
public static void main(String[] args) {
String mailSmtpHost = "cakelycakes.com";
String mailTo = "bigcakes@cakelycakes.com";
String mailCc = "littlecakes@cakelycakes.com";
String mailFrom = "me@here.there.everywhere";
String mailSubject = "Email from Java";
String mailText = "This is an email from Java";
sendEmail(mailTo, mailCc, mailFrom, mailSubject, mailText, mailSmtpHost);
}
public static void sendEmail(String to, String cc, String from, String subject, String text, String smtpHost) {
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
Session emailSession = Session.getDefaultInstance(properties);
Message emailMessage = new MimeMessage(emailSession);
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
emailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
emailMessage.setText(text);
emailSession.setDebug(true);
Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
解决方案
I think you're looking for these JavaMail FAQ entries: