0

I can send attachments with URLs, But, looking for support to send a file with an attachment. that too should download from url and attach with mail.

MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail@email.com");

//to mail address
mail.To.Add(txtEmail.Text);

//Add the Attachment
mail.Attachments.Add(data);

//set the content 
mail.Subject = txtSubject.Text;
mail.Body = "Kindly find the below attachments with the link, https://www.python.org/static/img/python-logo@2x.png";

//send the message 
SmtpClient smtp = new SmtpClient("*****.********.net");

NetworkCredential Credentials = new NetworkCredential("mymail@email.com", "********");
smtp.Credentials = Credentials;
smtp.Send(mail);

Here I'm sending mail with URL as a file, But I want to attach a file instead of sending a link

The sample URL was added as an image link.. But I wanted to add a pdf link.. In this case, I want to download a file and attach it with mail,

Gokul Raghu
  • 55
  • 2
  • 11

2 Answers2

1

I use it mostly in my all projects it's working fine. its with base 64

First, make sure your Gmail account is turned on for sending emails.

    public static async Task SendEmail(string toUser, string subject, string body
        , string username, string password, string port, string smtpServer, string ccUsers = "", string base64Url = "")
    {
        var toAddress = new System.Net.Mail.MailAddress(toUser, toUser);


        System.Net.Mail.MailMessage emessage = new System.Net.Mail.MailMessage();

        emessage.To.Add(toAddress);

        if (!string.IsNullOrEmpty(ccUsers))
        {
            string[] ccIds = ccUsers.Split(',');
            foreach (string item in ccIds)
            {
                emessage.CC.Add(new System.Net.Mail.MailAddress(item));
            }
        }

        emessage.Subject = subject;
        emessage.From = new System.Net.Mail.MailAddress(username);
        emessage.Body = body;
        emessage.IsBodyHtml = true;
        System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient();

        if (!string.IsNullOrEmpty(base64Url))
        {
            base64Url = base64Url.Replace("data:image/jpeg;base64,", "");
            var imageBytes = Convert.FromBase64String(base64Url);
            var stream = new MemoryStream(imageBytes);
            System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Image.Jpeg);
            System.Net.Mail.Attachment at = new System.Net.Mail.Attachment(stream, ct);
            at.ContentDisposition.FileName = "Invoice.jpeg";
            emessage.Attachments.Add(at);
        }


        var netCredential = new System.Net.NetworkCredential(username, password);
        sc.Host = smtpServer;
        sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        sc.UseDefaultCredentials = false;
        sc.Credentials = netCredential;
        sc.EnableSsl = true;
        sc.Port = Convert.ToInt32(port);
        await sc.SendMailAsync(emessage);

    }
DavSin
  • 86
  • 5
1

It works fine and mail directly reaches inbox instead of spam.. also can able to attach files from serverpath. Thank you everyone who wish to answer my questions..

    {
        try
        {

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("mymail@gmail.com");
            mail.To.Add("tomail@gmail.com");
            
            //set the content 
            string filename = "Report1";

            string fileurl = Server.MapPath("..");
            fileurl = fileurl + @"\mailserver\pdf\generated\" + filename + ".pdf";


            //mail.Attachments.Add(new Attachment(fileurl));
            
            if (!string.IsNullOrEmpty(fileurl))
            {
                Attachment attachment = new Attachment(fileurl, MediaTypeNames.Application.Pdf);
                ContentDisposition disposition = attachment.ContentDisposition;
                disposition.CreationDate = File.GetCreationTime(fileurl);
                disposition.ModificationDate = File.GetLastWriteTime(fileurl);
                disposition.ReadDate = File.GetLastAccessTime(fileurl);
                disposition.FileName = Path.GetFileName(fileurl);
                disposition.Size = new FileInfo(fileurl).Length;
                disposition.DispositionType = DispositionTypeNames.Attachment;
                mail.Attachments.Add(attachment);
            }

            mail.Subject = "Subject Text";
            mail.Body = "Body Text";

            //send the message 
            SmtpClient smtp = new SmtpClient("smtpout.****.******server.net");

            NetworkCredential Credentials = new NetworkCredential("mymail@gmail.com", "Password@123");
            smtp.Credentials = Credentials;
            
            //smtp.EnableSsl = true;
            smtp.Send(mail);
            
            Response.Write("<center><span style='color:green'><b>Mail has been send successfully!</b></span></center>");
        }
        catch (Exception ex)
        {
            //Response.Write("<center><span style='color:red'><b>"+ ex + "</b></span></center>");

            Response.Write("<center><span style='color:red'><b>Failed to send a mail...Please check your mail id or Attachment missing</b></span></center>");
        }
    }
Gokul Raghu
  • 55
  • 2
  • 11