the sandman
the sandman

Reputation: 1029

Format to include attachment in SendMail C#

I'm not sure how to add an attachment to my SendMail function. Lets say i want to add "C:\test.pdf" as an attachment to my email, how do i add this from the SendMail function?

My User Side Code:

string attachment = "c:\test.pdf";

objEmail.SendMail(EmailTo, EmailFrom, Subject, "", attachment, System.Net.Mail.MailPriority.High, "", "", true);

Function Code:

public void SendMail(string mailTo, string mailFrom, string mailSubject, string mailBody, string[] mailAttachment, System.Net.Mail.MailPriority mailImportance, string mailCC, string mailBCC, bool mailBodyType)

Upvotes: 1

Views: 334

Answers (2)

JYelton
JYelton

Reputation: 36512

If you are using the System.Net.Mail SmtpClient and System.Net.Mail.MailMessage classes, and wrapping them with your method, then you need to just use the MailMessage.Attachments property (an AttachmentCollection).

For example:

// specify the smtp connection
SmtpClient client = new SmtpClient("mail.yourdomain.com");

// create a MailMessage
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(new MailAddress("[email protected]");
mail.Subject = "EMail With Attachment";
mail.Body = "Please see attached document.";

// create an attachment
string pathToAttachment = @"\path\to\test.pdf";
mail.Attachments.Add(new Attachment(pathToAttachment));

// send the MailMessage
client.Send(mail);

Upvotes: 1

Oskar Kjellin
Oskar Kjellin

Reputation: 21880

Not sure what method SendMail is but you need to create an array like this:

string attachment = "c:\test.pdf";
string[] attachments =  new string[] { attachment };

Upvotes: 1

Related Questions