The outgoing SMTP is configured to the SharePoint 2010 server. Where as in my custom webpart, need to use the configured settings to send email from webpart itself. Is there any mechanism in object model to perform this. have not much idea for the same.
1 Answer
Try this method:
Without attachment using SPUtility.
private Boolean SendEmail()
{
try
{
bool flag = false;
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
using (SPSite site = new SPSite(
SPContext.Current.Site.ID,
SPContext.Current.Site.Zone))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
flag = SPUtility.SendEmail(web, true, true,
"[email protected]",
"Subject",
"This is a sample email Body");
}
}
});
return flag;
}
catch (System.Exception exp)
{
// Do some error logging
return false;
}
}
References:
- Send email with sharepoint
- https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.sendemail.aspx
- http://burnignorance.com/sharepoint-tips-and-hacks/send-email-in-sharepoint-using-sputility-sendemail-using-html-tags/
- https://www.collaboris.com/blogs/collaboris-blog/mark-jones/2012/11/06/code-sample-how-to-programmatically-send-an-email-in-sharepoint
UPDATE
One of the ways to send an e-mail from SharePoint as developer is to make use of the "SPUtility.SendEmail" classes. But unfortunately I did not find any possibility to include an attachment with the help of these classes.
You can easily use the following specified methods to send email with attachment.
Method I
Here we are going to use System.Net.Mail classes to send a message. Instead of hard coding the SMTP information into the code, we are going to use the SMTP settings that are configured with the SharePoint Central Administration. These settings are found in the SPAdministrationWebApplication class.
//Get the Sharepoint SMTP information from the //SPAdministrationWebApplication
string strSmtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
string strSmtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
//Create the mail message and supply it with from and to info
MailMessage objMailMessage = new MailMessage(strSmtpFrom,"[email protected]");
//Set the subject and body of the message
objMailMessage.Subject = "Subject: Testing Sending mail with attachment";
objMailMessage.Body = "This is Test Mail with a attachment";
//Download the content of the file with a WebClient
WebClient webClient = new WebClient();
//Supply the WebClient with the network credentials of our user
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
//Download the byte array of the file
byte[] data = webClient.DownloadData(insert_ attachment_url);
//Dump the byte array in a memory stream because
//we can write it to our attachment
MemoryStream memoryStreamOfFile = new MemoryStream(data);
//Add the attachment
objMailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, insert_Filenamefor_attachment, insert_content_type));
//Create the SMTP client object and send the message
SmtpClient objSmtpClient = new SmtpClient(strSmtpServer);
objSmtpClient.Send(objMailMessage);
Method II
MailMessage message = new MailMessage();
//Get the Sharepoint SMTP information from the //SPAdministrationWebApplication
message.From = new MailAddress(SPAdministrationWebApplication.Local.OutboundMailSenderAddress.ToString());
message.To.Add(new MailAddress("[email protected]"));
message.IsBodyHtml = true;
//Set the subject and body of the message
message.Body = "Hi there, check out the attachment";
message.Subject = "Sent Attachment";
using (SPWeb web = SPContext.Current.Web)
{
//Get the Document Library from where you want to sed the attachment
SPList splDataSpringsLibrary = web.Lists["MyDocuments"];
//Get the Url of the file to be sent as an attachment
string strUrl = "http://ocs-wks-029:2323/MyDocuments/Configuring Database Mial SQL server 2008.doc";
//Get the file to be sent as an attachment
SPFile file = splDataSpringsLibrary.ParentWeb.GetFile(strUrl);
//Add the attachment
message.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
//Create the SMTP client object and send the message
SmtpClient smtpClient = new SmtpClient(SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address);.
smtpClient.Send(message);
}
For Email with attachments please refer this:
- http://emailfromsharepoint.blogspot.in/2012/04/sending-e-mail-with-attachments-from.html
- http://edwin.vriethoff.net/2007/10/02/how-to-send-an-e-mail-with-attachment-from-sharepoint/
Hope this will help you.