简体   繁体   中英

Getting error:-System.net.mail.SmtpException

When running the below code, I get

Unhandled Exception: System.Net.Mail.SmtpException

This code is for sending mail. It works fine with Windows application, but I'm getting a runtime error on Mono for Android. Smeone told me that System.net.mail is part of the System.dll assembly, but I don't know how to use it in my MonoDroid application.

Additional namespace is: "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);

This runs fine on Windows. I'm running Mono for Android 4.2.7, Visual Studio 2010.

Try below one as Test and then change it to your situation If it's working .Good Luck.

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();
          }
        }
      }

You can get more details from Original Post

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM