获取错误:-System.net.mail.SmtpException

时间:2012-12-01 06:41:08

标签: c# xamarin.android

运行以下代码时,我得到了

  

未处理的异常:System.Net.Mail.SmtpException

此代码用于发送邮件。它适用于Windows应用程序,但我在Mono for Android上遇到运行时错误。 Smeone告诉我System.net.mail是System.dll程序集的一部分,但我不知道如何在我的MonoDroid应用程序中使用它。

其他命名空间是:“using System.Net.Mail;”

 string username = "abc@xyz.com";
 string password = "1234567890";
 System.Net.NetworkCredential nc = new
 System.Net.NetworkCredential(username, password);
 MailMessage MailMessage = new MailMessage();
 MailMessage.To.Add("pqr@xyz.com");
 MailMessage.Subject = "here is the subject";
 MailMessage.From = new System.Net.Mail.MailAddress("abc@xyz.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);

这在Windows上运行良好。我正在运行Mono for Android 4.2.7,Visual Studio 2010。

1 个答案:

答案 0 :(得分:1)

尝试低于一个作为测试然后根据您的情况更改它如果正常工作。好运。

using System;
using System.Net.Mail;
using System.Collections.Generic;
using System.Text;
using Gtk;
using GtkSharp;
using GLib;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace KentSoft
    {
    class  printTest : Window
    {
    public  printTest() : base("Kent_Calisma")
    {
    try{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("yourmailadress@gmail.com");
    mail.To.Add("destinationmailadress@gmail.com");
    mail.Subject = "TEST";
    mail.Body = "This is for testing SMTP mail from GMAIL";
    SmtpServer.Port = 587;

    SmtpServer.Credentials = new System.Net.NetworkCredential("gmailusername without @gmail.com", "gmailpassword");

    SmtpServer.EnableSsl = true;
    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

    SmtpServer.Send(mail);

}

catch(Exception e){
    Console.WriteLine("Ouch!"+e.ToString());
  }
}

public static void Main()
    {
   Application.Init();
   new printTest();
   Application.Run();
          }
        }
      }

您可以从 Original Post

获取更多详细信息