views:

30

answers:

1

'Microsoft.SharePoint.MailMessage' is inaccessible due to its protection level

On this code:

MailMessage mail = new MailMessage(); mail.From = "[email protected]"; mail.To = "[email protected]"; mail.Subject = "Testing Code"; mail.BodyText = what; mail.Priority = MailPriority.High; Smtp.Send(mail, "smtp.xxxxx.edu");

How can remidy this? changes to web.config? Any way to circumvent in code?

Thanks.

+1  A: 

This error is saying that MailMessage does not have public constructor. Most likely, it is for internal SharePoint use only.

Actually, in most cases in SharePoint you need to use SPUtility.SendEmail method to send mail with SharePoint. It is very simple:

SPUtility.SendEmail(SPContext.Current.Web, false, false, "[email protected]", "Testing Code", what);

See MSDN for details on this method: http://msdn.microsoft.com/en-us/library/ms411989.aspx

If you need to send email under ordinary user accounts, you should use SPSecurity.RunWithElevatedPrivilegies method to provide elevated privilegies.

Only disadvantage is that SPUtility does not support attachments. If you need attach some files to your letter, please use System.Net.Mail.

I know a good post from Edwin Vriethoff, which provide detailed information about sending email with attachments, with default SharePoint SMTP settings (they are configured through Central Administration): http://edwin.vriethoff.net/2007/10/02/how-to-send-an-e-mail-with-attachment-from-sharepoint/

omlin
That was it. Thanks!
cyberpine