Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Send emails with attachments error

$
0
0

Aim: to send a custom email message when a list item is added using an event receiver. The email should have a document attached, which will be from a different site within the same site collection.

The following code works (but obviously doesn't include an attachment):

/// Get Message information
MailMessage attachmentmessage = new MailMessage();
attachmentmessage.From = new MailAddress(from);
attachmentmessage.To.Add(new MailAddress("XXX@XXX.com"));
attachmentmessage.IsBodyHtml = true;
attachmentmessage.Body = body;
attachmentmessage.Subject = subject;

/// Send email with Attachments
SmtpClient smtpClient = new SmtpClient("XXX");
smtpClient.Send(attachmentmessage);

When I add the following code (in place of the line-break) I get an error and the message does not send:

/// Get Attachment
string url = "http://XXX/Site/Subsite/Documents/another%20test%20doucment.docx";
using (SPSite attachmentsite = new SPSite(url))
{
	using (SPWeb attachmentweb = attachmentsite.OpenWeb())
	{
		SPFile file = attachmentweb.GetFile(url);
		attachmentmessage.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
	}
}

Can you provide any advice?


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>