Monday, April 07, 2008

send email in sharepoint 2007

Hi Folks,

if you are developing a webpart or any sharepoint customized feature,and you would like to send an email through your webpart or custom code,you can achieve this by 2 ways:

1) use normal .net classes to send email (System.Net.Mail namespace).
2) use below code:

using Microsoft.SharePoint.Utilities;

SPUtility.SendEmail(SPContext.Current.Web, false, false,
"to-address@mail.com", "E-mail title",
"E-mail body");

this code will use the configuration of your sharepoint installation (SMTP server configuraiton),in the central administraiton tool.

the benefit of the second choice,you don't need to store any other configuration for your SMTP server on your code,this code will directly use the sharepoint configuration and free your mind of SMTP configurations, if the sharepoint is configured propely and send email notification (throught workflows for example),then you code will work as well :).


hope this trick will help.

Regards,
Mostafa arafa

20 comments:

Smruti Thinks said...

superb!!!i was really struggling with .net way , configuration...even tried a dumm SMTP......
Thanks

Abhay K said...

How to send mail to multiple recipient .

Anonymous said...

Hi, I have question regard the SPUitlty.SendMail.

I tried use this method and the email has successfully send to my mail box, however, my outlook automatically filter the email as a junk mail and put it to the junk mail folder.
Is there a way to prevent the mail get send to the junk mail box?

Anonymous said...

Do you know how I can get the "reply to address" in SharePoint? I mean the one where I configured outgoing e-mail settings. I would like to use that address when I send an e-mail.

Unknown said...

Please let me know how to send mail to multiple users

Unknown said...

Hi All,

In the To string variable put the email addresses separated by semi colons.

for example:

"x@x.com;y@y.com;z@z.com"

let me know if you need any other help.

Anonymous said...

Hi Moustafa,

Please let me know if we can have a system where in we can send/receive messages just like outlook, attach signatures, attach files etc.

Savan Dounde said...

Thanks...It helped me a lot.

Nag said...

I dont want to use outlook i want to send email to gmail.com or yahoo.com or any other.

How it is possible is there any solution.

Nag said...

I dont want to use outlook i want to send email to gmail.com or yahoo.com or any other.

How it is possible is there any solution.

Saurabh said...

Hi...

it doesn't work.....

SPUtility.SendEmail(SPContext.Current.Web, false, false,
"to-address@mail.com", "E-mail title",
"E-mail body");

i tried this in CodeActivity of Workflow.
but didn't get any mail....
and workflow History log doesn't show any error

Kind Regards,
Saurabh

Unknown said...

Hi,

Make sure that SharePoint SMTP configuration is setup correctly,you can do that from sharepoint admin website.

Amila Abeyrathne said...

to send multiple recipient you have to do this in a loop. send mail example and outgoing e mail settings can be viewed from below links

http://sharepoint-amila.blogspot.com/2009/03/auto-generated-email-for-sharepoint.html

http://sharepoint-amila.blogspot.com/2008/02/outgoin-email-settings.html

Anonymous said...

Beware, there seems to be a limitation of 2048 caracters when using Sharepoint to send emails (SPutility.sendmail, etc). I have experienced this problem using the sendEmail activity in a custom workflow. I will just use System.Net.Mail from now on.

Another user also posted about it here: http://social.technet.microsoft.com/forums/en-US/sharepointworkflow/thread/43f48e67-02d9-42d2-b51a-d10c06213783/

Marshal Nagpal said...

Do you have any idea how to send email using JQuery in sharepoint?

Anonymous said...

Excellent article...

Mohamed hachem said...

I had similar problem, it is all Encoding, when I tried to attach files with names including French characters. I receive an error
"An invalid character was found in the mail header."

When I looked at the code there was this line :
attachment.ContentDisposition.FileName = uxd_SendFilePath_FileUpload.FileName;

this property allows the sender to suggest the name to be used to store an e-mail attachment on the recipient's computer. This name is a suggestion only; the receiving system can ignore it. The name must not include path information; any such information is ignored by the receiving computer.

Though it is not UTF8 encoded so if you have file name in spanish, french, german, bosnian, etc you will automatically receive an error.

Solution is very simple :
replace
attachment.ContentDisposition.FileName = FileUpload.FileName;

by

attachment.Name = FileUpload.FileName;
attachment.NameEncoding = Encoding.UTF8;

Hence, the user will be able to upload files with names holding weird characters, viva multiculturalism :)

Cheers

أحمد بن عمر باجابر said...

It helps so much!

Thanks for the great post

Rahul said...

I want to send a Daily Update mail to my boss. I am adding entries to a SharePoint list eevryday. i created a view "Daily" where it shows the entries added on that day only. I want to pull the data from this view and send it to my boss everyday automatically. Is there any way to do this?

Unknown said...

Yes you can do this by reading list items for daily view and send the email based on captured email addresses.

Let me know if you want any other help.