0
votes

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

2

2 Answers

1
votes

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.
1
votes

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