MimeMessage fordward = new MimeMessage(this.session); fordward.setFrom((Address)new InternetAddress(this.username)); System.out.println("发件人:" + this.username); fordward.setSubject(subject); System.out.println("收件人邮箱:" + receiveAddress); fordward.setRecipients(Message.RecipientType.TO, (Address[])InternetAddress.parse(receiveAddress)); System.out.println("收件人邮箱:" + receiveAddress); MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); String str1=getBodyText().substring(0, getBodyText().indexOf("Dear")); String text=getBodyText().substring(str1.length(), getBodyText().length()); /* mbp.setContent( getBodyText(), "text/html;charset=utf-8");*/ mbp.setContent( text, "text/html;charset=utf-8"); System.out.println("邮件内容:" + text); mimeMultipart.addBodyPart((BodyPart)mbp); fordward.setContent((Multipart)mimeMultipart); fordward.setSentDate(new Date()); fordward.saveChanges(); trans.sendMessage((Message)fordward, fordward.getAllRecipients()); 怎么判断邮件是否成功发送给对方
时间: 2023-11-27 18:50:09 浏览: 57
邮件发送接收
您可以通过捕获异常来判断邮件是否成功发送给对方。在发送邮件的代码块中,如果出现异常,说明邮件发送失败;否则,邮件发送成功。以下是一个示例代码:
```java
try {
// 发送邮件的代码块
// ...
System.out.println("邮件发送成功");
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("邮件发送失败");
}
```
阅读全文