1

I'm trying to send an email using SMTP-client. I need to send an image as an attachment. Now, the image is saved as a base64 string. The email can be sent without an attachment. But when I try to add an attachment, the email cannot be sent.

Here, financeMediaPath is the string that contains my base64 image URL. The image can be saved in any format. I just want to attach that image to my email.

public void ExpenseAdded(Email message, string toEmail, string firstName, string lastName, string admin, FinanceEntity finance, string financeCategory, string financeMediaPath)
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress(username);
        mail.To.Add(toEmail);
        string url = "http://localhost:4200/#/currentRequests";

        mail.Subject = "Mobile Team Reimbursement " + firstName + " - " + finance.RequestedDate + " " + financeCategory;
        mail.IsBodyHtml = true;


        mail.Body = "Hi " + admin + ",<br><br>" +
                    firstName + " has entered a new expense in HR portal.<br>" +
                    "<ul><li> Name: '" + firstName + " " + lastName + "'</li><li> Category: '" + financeCategory + "'</li><li> Project: '" + finance.ProjectName + "'</li><li> Total Amount: '" + finance.TotalAmount + "'</li><li> Description: '" + finance.Description + "'</li><li> Account Details: '" + finance.AccountNumber + " " + finance.AccountOwner + " " + finance.Branch + "'</li></ul>" +
                    "<br><br>Message: " + finance.Message + "<br><br>Please <a href = '" + url + "'>click here</a> to view all current pending expense requests." + "<img src='data:image/jpeg;base64, <!-- base64 data --> />";

        Attachment attachment = new Attachment(financeMediaPath);
        attachment.TransferEncoding = TransferEncoding.Base64;
        mail.Attachments.Add(attachment);


        SmtpClient smtp = new SmtpClient();

        smtp.Port = 587;
        smtp.Host = smtpServer;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = new NetworkCredential(username, password);
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtp.Send(mail);

    }
  • Not sure what you are trying to do here - why would you need to store the Base64 string as a file and then attach it if you could simply add an ``tag with the base64 encoded image as its source? See [How to display Base64 images in HTML?](https://stackoverflow.com/q/8499633/205233) – Filburt May 04 '21 at 16:19
  • Amime attachment starts on a new line with two dashes. See following for an example : https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/aa563375(v=exchg.140)?force_isolation=true – jdweng May 04 '21 at 16:20
  • @Filburt I edited the question. Can you understand it now? – Pavara Manupriya May 04 '21 at 20:43
  • Possibly duplicated: https://stackoverflow.com/questions/51707782/c-sharp-base64-in-email-attachment – Fernando Lopes Jun 08 '21 at 17:04

0 Answers0