在javamail中添加setFrom时未处理的异常类型MessagingException(unhandled exception type MessagingException when addi

编程入门 行业动态 更新时间:2024-10-19 05:19:45
在javamail中添加setFrom时未处理的异常类型MessagingException(unhandled exception type MessagingException when adding setFrom in javamail) MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com"));

在最后两行中获取unhandled exception type MessagingException 。

Eclipse建议我用try catch块覆盖它们。但是当我尝试在互联网上搜索时,他们正在使用这两个语句而没有任何尝试catch块。 所以我想我做错了什么。 怎么解决这个?

MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com"));

Getting error in last two lines that unhandled exception type MessagingException.

Eclipse suggests me to cover them with a try catch block.But when i tried to search in internet they are using these two statement without any try catch block. so i think i am doing something wrong. how to solve this?

最满意答案

你必须有可能解决它。

在代码中添加try-catch-block:

尝试{MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(“xyz@example.com”)); msg.addRecipient(Message.RecipientType.TO,new InternetAddress(“abcd@example.com”));

} catch(MessagingException me){//做某事}

throws MessagingException到方法签名:

public void mymethod throws MessagingException {MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(“xyz@example.com”)); msg.addRecipient(Message.RecipientType.TO,new InternetAddress(“abcd@example.com”)); }

You have to possibilities to solve it.

Add a try-catch- block arround your code:

try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com"));

} catch (MessagingException me){ // do something }

Add throws MessagingException to the method signature:

public void mymethod throws MessagingException { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com")); }

更多推荐

本文发布于:2023-08-07 13:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   类型   未处理   setFrom   javamail

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!