How to send a mail in Xamarin using System.Net.Mail.SmtpClient

末鹿安然 提交于 2019-12-11 19:23:57

问题


I am trying to send and email for inside an app using system.Net.Mail.SmtpClient. When I run the code on my phone I get a java.lang.runtimeexception error and I can't figure out why?

I have the following code to run send the email when I click a button.

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.ComponentModel;

namespace SendEmail
{
[Activity (Label = "SendEmail", MainLauncher = true)]
public class Activity1 : Activity
{

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        EditText Text = FindViewById<EditText> (Resource.Id.MailText);

        button.Click += delegate {

             string username = "****@gmail.com";
             string password = "****";
             System.Net.NetworkCredential nc = new
             System.Net.NetworkCredential(username, password);
             MailMessage MailMessage = new MailMessage();
             MailMessage.To.Add("****@gmail.com");
             MailMessage.Subject = "here is the subject";
             MailMessage.From = new System.Net.Mail.MailAddress("****@gmail.com");
             MailMessage.Body = "Application run time was ";
             System.Net.Mail.SmtpClient SmtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com");
                 SmtpClient.UseDefaultCredentials = false;

                 SmtpClient.EnableSsl = true;
                 SmtpClient.Credentials = nc;
                 SmtpClient.Port = 587;
            SmtpClient.Send(MailMessage);

        };
     }
}
}

回答1:


Try this it works well for me. ;)

    try
    {
        MailMessage mail=new MailMessage();
        SmtpClient SmtpServer=new SmtpClient("smtp.gmail.com");
        mail.From=new MailAddress("from address here");
        mail.To.Add("to adress here");
        mail.Subject = "Message Subject";
        mail.Body = "Message Body";
        SmtpServer.Port = 587;
        SmtpServer.Credentials=new System.Net.NetworkCredential("username","password");
        SmtpServer.EnableSsl=true;
        ServicePointManager.ServerCertificateValidationCallback=delegate(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) {
            return true;
        };
        SmtpServer.Send(mail);
        Toast.MakeText(Application.Context, "Mail Send Sucessufully", ToastLength.Short).Show();
    }

     catch(Exception ex) 
     {
         Toast.MakeText(Application.Context,ex.ToString(),ToastLength.Long);
     }


来源:https://stackoverflow.com/questions/30255789/how-to-send-a-mail-in-xamarin-using-system-net-mail-smtpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!