首页 > 解决方案 > 在 Xamarin Forms 中使用邮件服务时,我们如何获取 SmtpServer NetworkCredentials?

问题描述

我正在Forgot password我的 Xamarin Forms 应用程序中实现一个简单的功能。当玩家email在文本字段中提供一个时,将检查该电子邮件是否存在于数据库中,如果存在,将获取与该电子邮件关联的密码并将密码发送到该电子邮件。

在下面的 smtp 步骤SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");中,我们从哪里获取凭据以Network credentials作为电子邮件/密码提供?

namespace soccerapp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ForgotPassword : ContentPage
    {
        private SQLiteConnection conn;
        string emailText;

        public ForgotPassword()
        {
            InitializeComponent();
            conn = DependencyService.Get<Isqlite>().GetConnection();
        }

        public void btnSend_Clicked(object sender, EventArgs e)
        {
            emailText = txtEmail.Text;

            int count = (from x in conn.Table<PlayerDetails>().Where(x => x.Email == emailText) select x).Count();

            if (count != 0)
            {
                var detail = conn.Table<PlayerDetails>().First(x => x.Email == emailText);
                var playerPassword = detail.Password;
                try
                {

                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                    mail.From = new MailAddress("admin@somesite.nz");
                    mail.To.Add(emailText);
                    mail.Subject = "Hello";
                    mail.Body = "Your soccer password: "+playerPassword;

                    SmtpServer.Port = 587;
                    SmtpServer.Host = "smtp.gmail.com";
                    SmtpServer.EnableSsl = true;
                    SmtpServer.UseDefaultCredentials = false;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("some_email@gmail.com", "password");

                    SmtpServer.Send(mail);
                }
                catch (Exception ex)
                {
                    DisplayAlert("Faild", ex.Message, "OK");
                }
            }
        }
    }
}

标签: xamarinxamarin.formssmtpclient

解决方案


推荐阅读