I am developing an App in Xamarin for android which generates a PDF that is saved in a hidden folder of the device.
I currently use a WebService
to send the email but it does not arrive with the attached PDF.
I'm using a WebService
because I need this email to be sent automatically without any interaction with the user and the examples I had found on the internet usually opened gmail or another dialog which asked the user to send the email.
The exception that debug me back is:
System.IO.IOException: The network path was not found.
Obviously this exception occurs because the WS will pass the location (on the device) of the PDF as an argument and the WS can not find that file since the received location does not exist on the server where the WS is located.
My question is How can I pass the PDF to the WebService and send the attachment?
I leave here the code that I use to send the mail:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;
namespace Servicios.Clases
{
public class cmSendMailCcopy
{
public string mailTo;
public string mailCopy;
public string mailFrom;
public string mailSubject;
public string mailBody;
public string mailAuthentication;
public string mailPassword;
public string mailSmtpServer;
public void Send(string mailTo, string mailCopy, string mailFromAddress, string mailFromName,
string mailSubject, string mailBody, string mailAuthentication, string mailPassword,
string mailSmtpServer, int mailPort, List<string> mailAttachment = null)
{
try
{
MailMessage mail = new MailMessage();
string mailDe = mailFromAddress;
string deNombre = mailFromName;
string mailPara = mailTo;
string mailCopia = mailCopy;
string asunto = mailSubject;
string mensaje = mailBody;
string mailAutenticacion = mailAuthentication;
string mailContra = mailPassword;
string mailSmtp = mailSmtpServer;
int mailPuerto = mailPort;
List<string> mailAdjunto = mailAttachment;
try
{
SmtpClient smtp = new SmtpClient
{
Host = mailSmtp,
Port = mailPuerto,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(mailDe, mailContra),
Timeout = 3000
};
MailMessage correo = new MailMessage(mailDe, mailPara, asunto, mensaje);
correo.IsBodyHtml = true;
correo.CC.Add(mailCopia);
if (mailAdjunto != null){
foreach (var item in mailAdjunto)
{
if (System.IO.File.Exists(item)){
correo.Attachments.Add(new Attachment(item, MediaTypeNames.Application.Pdf));
}
}
}
smtp.Send(correo);
}
catch (Exception)
{
throw;
}
}
catch (Exception exc)
{
throw (exc);
}
}
}
}