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

Send mail from my SMTP Client in xamarin

我尝试用我的 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: The request feature is not implemented

但我将此代码与另一个 smtp 一起使用并且工作正常。有帮助吗?

您似乎无法在 Xamarin.xml 中使用 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);
    }
}

然后从 Forms 代码中调用整个过程,例如:

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

作为替代方案,您可以使用我的 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);
    }
}