我在下面写了一个程序来发送邮件通过java邮件api发送邮件现在我的查询是处理特殊情况也可以说如果邮件没有发送然后我必须要什么,如果邮件是发送我在那个cas中必须做一些其他的事情,现在请建议java mail api在发送邮件购买时证明我们任何参数我们可以检查邮件是否已成功发送,因为我已在程序中启用了调试
emailSession.setDebug(true);
请告知哪些是java mail api发送的返回参数,我们可以检查邮件是否成功发送
下面是我简单的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();
}
}
}
答案 0 :(得分:6)
我认为您正在寻找这些JavaMail FAQ条目:
答案 1 :(得分:0)
有一种方法可以发送所有电子邮件。
代表前 -
//create the POP3 store object and connect with the pop server
Store store = emailSession.getStore("pop3s");
store.connect(host, user, password);
//create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
System.out.println("messages.length---" + messages.length);
for (int i = 0, n = messages.length; i < n; i++) {
Message message = messages[i];
System.out.println("---------------------------------");
System.out.println("Email Number " + (i + 1));
System.out.println("Subject: " + message.getSubject());
System.out.println("From: " + message.getFrom()[0]);
System.out.println("Text: " + message.getContent().toString());
}
//close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
您可以通过比较电子邮件内容来查看邮件是否已发送的消息。
我希望它会对你有所帮助。
答案 2 :(得分:0)
我不确定这是你需要的。但您可以像这样检查SMTP服务器的返回码:
// get your configuration (host, port, user, pwd)
...
// initialize your message
...
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(host, port, user, pwd);
transport.sendMessage(message, message.getAllRecipients());
// you can get SMTP return code here
int code = transport.getLastReturnCode();