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