从我在xamarin的SMTP客户端发送邮件

本文关键字:客户端 SMTP xamarin | 更新日期: 2023-09-27 18:22:08

我尝试用smtp客户端发送邮件,但没有异常,邮件也没有收到。

public void SendSMTPMail(string from, string to, string subject, string body)
{
   var smtp_client = new SmtpClient("mail.mydomain.gr",25);
   smtp_client.UseDefaultCredentials = false;
   smtp_client.EnableSsl = false;
   smtp_client.Credentials = new NetworkCredential("noreply@mydomain.gr", "mypass");
   ServicePointManager.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
   var msg = new MailMessage(from, to );
   msg.Subject = subject;
   msg.Body = body;
   smtp_client.SendAsync(msg , string.Empty);
}

我使用断点,我发现一些信息

smtp_client.ServicePoint System.NotImplementException:请求功能未实现

但我将此代码与另一个smtp一起使用,效果很好。有什么帮助吗?

从我在xamarin的SMTP客户端发送邮件

作为替代方案,您可以使用我的MailKit库使用Xamarin.iOS/Android/Mac.发送邮件

public void SendSMTPMail(string from, string to, string subject, string body)
{
    var message = new MimeMessage ();
    var builder = new BodyBuilder ();
    message.From.Add (InternetAddress.Parse (from));
    message.To.Add (InternetAddress.Parse (to));
    message.Subject = subject;
    builder.TextBody = body;
    message.Body = builder.ToMessageBody ();
    using (var client = new SmtpClient ()) {
        client.ServerCertificateValidationCallback = (s, certificate, chain, sslPolicyErrors) => true;
        client.Connect ("mail.mydomain.gr", 25, false);
        client.Authenticate ("noreply@mydomain.gr", "mypass");
        client.Send (message);
        client.Disconnect (true);
    }
}

您似乎无法在Xamarin中使用System.Net.Mail.SmtpClient。相反,您应该使用带有本机实现的邮件服务。这里有个小例子。表单代码:

public abstract class EmailService
{
    public static readonly Lazy<EmailService> Instance = new Lazy<EmailService>(() => DependencyService.Get<EmailService>());
    public abstract bool CanSend { get; }
    public abstract void ShowDraft(string subject, string body, bool html, string to, byte[] screenshot = null);
    public abstract void ShowDraft(string subject, string body, bool html, string[] to, string[] cc, string[] bcc, byte[] screenshot = null);
}

本机iOS代码:

public class EmailServiceIos : EmailService
{
    public override bool CanSend
    {
        get
        {
            return MFMailComposeViewController.CanSendMail;
        }
    }
    public override void ShowDraft(
        string subject,
        string body,
        bool html,
        string[] to,
        string[] cc,
        string[] bcc,
        byte[] screenshot = null)
    {
        var mailer = new MFMailComposeViewController();
        mailer.SetMessageBody(body ?? string.Empty, html);
        mailer.SetSubject(subject ?? string.Empty);
        mailer.SetCcRecipients(cc);
        mailer.SetToRecipients(to);
        mailer.Finished += (s, e) => ((MFMailComposeViewController)s).DismissViewController(true, () => { });
        if (screenshot != null)
        {
            mailer.AddAttachmentData(NSData.FromArray(screenshot), "image/png", "screenshot.png");
        }
        UIViewController vc = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (vc.PresentedViewController != null)
        {
            vc = vc.PresentedViewController;
        }
        vc.PresentViewController(mailer, true, null);
    }
    public override void ShowDraft(string subject, string body, bool html, string to, byte[] screenshot = null)
    {
        this.ShowDraft(subject, body, html, new[] { to }, new string[] { }, new string[] { }, screenshot);
    }
}

并从表单代码中调用整个内容,如:

                    var emailService = DependencyService.Get<EmailService>();
                    if (emailService.CanSend)
                    {
                        emailService.ShowDraft(
                                    "Your caption",
                                    "Your text",
                                    true,
                                    "your@ddress.com");
                    }