如何处理 Messaging.sendEmail() 中的错误?

发布于 2024-10-03 01:15:31 字数 1333 浏览 5 评论 0原文

我编写了发送电子邮件的代码。它工作正常,但我的目标是:

当有人发送到不存在的电子邮件地址时,我想将结果记录为“错误”或“失败”等(当电子邮件地址有效时,只需说“成功”)

我已经使用下面的代码尝试了两件事。

  1. 提供了非电子邮件字符串“foo@!”

  2. 提供的电子邮件地址不存在 '[电子邮件受保护]'< /p>

< strong>结果:

执行情况1导致代码进入catch块,从而在html页面上输出预期的错误消息。

执行案例 2 导致代码返回 'ok sent!'

几分钟后,我收到了发送失败的电子邮件。

我的猜测是 SendEmailResult 对象的 isSuccess() 并不真正负责不存在的电子邮件地址检查。它只关心电子邮件是否被解雇???

如果电子邮件帐户不存在,是否有任何方法可以记录,以便我可以在我的 Apex 代码中记录此类情况?

try {
    Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new
            Messaging.SingleEmailMessage[] { mail });

    resultMail[0].getErrors();

    //display success or error message
    if (resultMail[0].isSuccess()) {
        response = 'ok sent!';
    } else {
        response = resultMail[0].getErrors().get(0).getMessage();
    }

    //log
    boolean isSuccess = resultMail[0].isSuccess();
    Integer out = EmailLogger.logEmailSent(this, isSuccess);
} catch (System.EmailException ex) {
    system.debug('============== email exception caught!!!=============');
    response = ex.getMessage();
}

I wrote code to send email. It works fine but my goal is:

When someone sent to non-existing email address, I want to log the result as 'false' or 'failure' etc (and when email address is valid, just say 'success')

I've tried 2 things with the code below.

  1. provided non-email string 'foo@!'

  2. provided non-existing email address '[email protected]'

result:

Execute case 1 caused code to go into catch block thus outputting error message on the html page which is expected.

Execute case 2 caused code to return 'ok sent!'

And after few minutes I received email that delivery failed.

My guess is SendEmailResult object's isSuccess() is not really responsible for non-existing email address check. It only cares if the email is fired???

Is there any way to log if the email account does not exist so I can log such occasion in my Apex code?

try {
    Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new
            Messaging.SingleEmailMessage[] { mail });

    resultMail[0].getErrors();

    //display success or error message
    if (resultMail[0].isSuccess()) {
        response = 'ok sent!';
    } else {
        response = resultMail[0].getErrors().get(0).getMessage();
    }

    //log
    boolean isSuccess = resultMail[0].isSuccess();
    Integer out = EmailLogger.logEmailSent(this, isSuccess);
} catch (System.EmailException ex) {
    system.debug('============== email exception caught!!!=============');
    response = ex.getMessage();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-10-10 01:15:31

电子邮件(SMTP)是一种存储转发协议,在发送时,您无法得知目标电子邮件地址不存在,只有当邮件真正到达最终目标服务器时才能发现。

Email (SMTP) is a store and forward protocol, at the time of sending, you can't tell that the destination email address is non-existant, you can only find that out once the message actually gets to the final destination server.

断舍离 2024-10-10 01:15:31

如果有办法找到电子邮件地址,无论它是否真的存在,垃圾邮件发送者可能会尝试暴力攻击 - 尝试所有可能的电子邮件组合并发送无限的垃圾邮件:)

感谢上帝,这是不可能的。

if there was a way to find email address whether it really exists or not, a spammer might have tried brute force attack - trying every possible combination of email and sending infinite spams :)

thank god, that's not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文