Is there a way to send an e-mail without using an App in Xamarin.forms?

1

Recently I'm programming in Xamarin.forms and I have an App that by pressing a button you have to send an e-mail to a predefined email.

Searching the internet I found the NuGet Xam.Plugins.Messaging which works, but that makes the App open to send the mail (For example: gmail) and what I need is that the mail is sent directly without having to go through for an App previously.

Is there such a possibility?

Here I leave what the button does:

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Plugin.Messaging;

namespace PruebaCorreo
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Chat : ContentPage
    {
        public Chat()
        {
            InitializeComponent();
        }

        private void BtnMail_Clicked(object sender, EventArgs e)
        {
            var emailTask = CrossMessaging.Current.EmailMessenger;
            if (emailTask.CanSendEmail)
            {
                // Envia un correo simple.
                emailTask.SendEmail("[email protected]", "Asunto", "Cuerpo del mail");

                // Envia un correo con mas datos.
                var email = new EmailMessageBuilder()
                .To("[email protected]")
                .Cc("[email protected]")
                .Subject("Asunto del correo")
                .Body("Este es el cuerpo del correo")
                .Build();

                emailTask.SendEmail(email);
            }
        }
    }
}
    
asked by Matias 15.01.2018 в 17:11
source

1 answer

1

Try this way, there you put the port the message that you are going to send in the mail this data is for gmail

try
{
    MailMessage mail=new MailMessage();
    SmtpClient SmtpServer=new SmtpClient("smtp.gmail.com");
    mail.From = new MailAddress("El correo del destinatario");
    mail.To.Add("tu dirección de correo");
    mail.Subject = "Asunto";
    mail.Body = "Cuerpo del mensaje";
    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, "Email enviado exitosamente", ToastLength.Short).Show();
}

 catch(Exception ex) 
 {
     Toast.MakeText(Application.Context,ex.ToString(),ToastLength.Long);
 }
    
answered by 15.01.2018 / 18:25
source