簡體   English   中英

從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:請求功能未實現

但是我將這個代碼與另一個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);
    }
}

然后從Forms代碼中調用整個過程,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM