views:

342

answers:

2

Hi,

I am trying to send an email in the button click event in the webpart.However, the email sending is failed and error is logged in both sharepoint logs and event viewer of the machine. The error is : Cannot connect to SMTP host

Outgoing email settings in central admin is already configured. Alerts are also working fine.

Below is my code:

 StringDictionary headers = new StringDictionary();
 headers.Add("to", "[email protected]");
 headers.Add("from", "[email protected]");
 headers.Add("subject", "How to use SendEMail from SPUtility");
 headers.Add("content-type", "text/html"); 
 string bodyText = "This is the body of my email, in html format.";
 SPUtility.SendEmail(SPContext.Current.Web, headers, bodyText);

Thanks in advance

A: 

Try running your code without the System Account (if you are).

Try also disabling the LoopbackCheck:

To set the DisableLoopbackCheck registry key, follow these steps:

  • Click Start, click Run, type regedit, and then click OK.
  • In Registry Editor, locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
  • Right-click Lsa, point to New, and then click DWORD Value.
  • Type DisableLoopbackCheck, and then press ENTER.
  • Right-click DisableLoopbackCheck, and then click Modify.
  • In the Value data box, type 1, and then click OK.
  • Quit Registry Editor, and then restart your computer.
F.Aquino
I got that fixed. Anti virus was causing the problem. But i am wondering how is SharePoint able to send mail then?
Faiz
local loopback check?
Ryan
A: 

I got that fixed. Anti virus was causing the problem. But i am wondering how is SharePoint able to send mail then?

Faiz
Probably because SharePoint alert me emails are fired off from the SharePoint timer service and your code is running under ASP.NET w3wp and your Anti-Virus had been set to allow the timer process.
Ryan
Good thought, thanks.
Faiz