【问题标题】:Xamarin, get error sending mail using SmtpClientXamarin,使用 SmtpClient 发送邮件时出错
【发布时间】:2021-03-22 17:40:44
【问题描述】:

我正在使用 System.Net.Mail.SmtpClient 在我的 Xamarin Form 应用程序中发送邮件。它设置为使用我的 gmail 地址,并且运行良好。

我想从 smtp 服务器(如果有)获取错误以通知用户。

所以,我正在使用事件 _SendCompleted

这是我的代码

(发送电子邮件)

 MailMessage mail = new MailMessage();
                        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
                        
                        mail.From = new MailAddress(outils.emailEmetteur);
                        mail.To.Add("toto@outlook.frfr");//Tiers.contactEmail);
                        mail.Subject = outils.emailObjetDefaut;
                        mail.Body = outils.emailMessageDefaut;

                        bool fileExist = File.Exists(filePath);
                        if (fileExist)
                        {
                            Attachment pJ = new Attachment(filePath);
                            mail.Attachments.Add(pJ);

                            smtpServer.Port = 587;
                            smtpServer.Host = "smtp.gmail.com";
                            smtpServer.EnableSsl = true;
                            smtpServer.UseDefaultCredentials = false;
                            smtpServer.Credentials = new NetworkCredential(outils.emailEmetteur, outils.emailEmetteurMDP);

                            try
                            {
                                smtpServer.SendAsync(mail, null);
                                smtpServer.SendCompleted += smtpServer_SendCompleted;

                                return true;
                            }
                            catch (Exception ex)
                            {
                               
                            }
                        }

(发送完成的事件)

 private static void smtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            string token = (string)e.UserState;
            string info = "";

            if (e.Cancelled)
            {
                info = "Send canceled.";
            }
            if (e.Error != null)
            {
                info = e.Error.ToString();
            }
            else
            {
                info = "Message sent.";
            }
        }

我正在尝试向不正确的地址发送电子邮件 (toto@outlook.frfr)

我的事件被正确触发,但 e.Cancelled 和 e.Error 为 NULL,并且在我的 Gmail 收件箱中,我收到来自 smtp 服务器的错误,告诉我电子邮件地址不正确,以及我想要获取的内容在我的 Xamarin 应用程序中。

你有什么想法吗?谢谢:)

【问题讨论】:

    标签: email xamarin error-handling smtp smtpclient


    【解决方案1】:

    您的客户端将其中继到 Gmail 的外发邮件服务器;将消息提交到 SMTP 的事务成功。该错误仅在稍后发生,当 Gmail 尝试连接到收件人的邮件服务器但无法解决时;此时,您的客户端不再需要连接,因此邮件服务器会生成退回邮件并将其发送到发件人的收件箱。

    在一般情况下,您要么必须编写自己的邮件服务器(但不,不要去那里)或检查收件箱中的退回邮件。

    【讨论】:

    • 哦,好的,谢谢!那么,没有办法在我的应用程序中获取错误消息吗?我试图找到一种不使用 smtp 发送电子邮件的方法,但没有找到任何东西
    • Gmail 有一个 HTTPS API developers.google.com/gmail/api/guides/sending 但我不熟悉它的细节。快速浏览一下,它似乎并没有提供任何直接的补救措施。
    猜你喜欢
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 2022-06-13
    • 2013-05-20
    • 2011-01-29
    相关资源
    最近更新 更多