在Mac C#上使用Xamarin Studio中的MailKit发送电子邮件

时间:2015-10-22 18:51:01

标签: c# android xamarin xamarin-studio mailkit

我正在尝试在Xamarin Studio for Mac中编写一个Android应用程序,只要点击一个按钮就会发送一封电子邮件。我有按钮,如果我不尝试发送电子邮件,则单击按钮时应用程序将运行。但是,当我添加下面代码中显示的MailKit命令时,应用程序每次都会崩溃。任何帮助将不胜感激!

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Net;
using MimeKit;
using MailKit; 
using MailKit.Net.Smtp; 

namespace MyProject
{
    [Activity (Label = "MyProject", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : 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.btnOne); 
        TextView text = (TextView) FindViewById(Resource.Id.textView1); 
        button.Click += delegate {




            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("User Name", "username@gmail.com"));
            message.To.Add (new MailboxAddress ("Other User", "otheruser@gmail.com"));
            message.Subject = "Does it work???";


            var builder = new BodyBuilder (); 

            builder.TextBody = "Hello, world!"; 
            message.Body = builder.ToMessageBody (); 

            using (var client = new SmtpClient ()) {

                client.Connect ("smtp.gmail.com", 465, true); 

                client.Authenticate ("username", "password"); 

                client.Send (message); 
                client.Disconnect (true); 

            }

        };

    }
}
}

0 个答案:

没有答案