'Send Email in Umbraco

I am setting up a function to send out mails(as confirmation for orders but thats a later matter).

Problem is that the mails are to be sent from the companys exchange server and so far all i have gotten is connection failed.

I am in communication with the server tech trying to find out whats wrong, and since its been a while since i set up email(on the cms side) i might be making a stupid mistake, so maybe somebody here will catch it :)

web.config: (port number specified by server tech)

(all info replaced with generic but consistent names for safety ofc)

<system.net>
<mailSettings>
    <smtp from="[email protected]">
        <network host="smtp.ourexchangeserver.dk" userName="[email protected]" password="secret" port="26" />
    </smtp>
</mailSettings>

razor script test:

      @{
    try {
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("[email protected]");

message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Send(message);
    }
    catch(Exception ex)
            {
               <p> error: <br/> @ex.ToString() </p> 
            }
}

Error caught in exception:

 System.Net.Mail.SmtpException: Failure sending mail. --->
 System.Net.WebException: Unable to connect to the remote server --->
 System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Thank you in advance.



Solution 1:[1]

Could you please confirm that they are giving you the port credentials are of exchange server's and not of Exchange Web services Managed API.

Second is that you would require to specify Fields when sending emails using SMTP server based on the configurations. e.g

   OMsg1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp"; // ypur SMTP serner name

        OMsg1.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = 25;

and like wise.

Please find below link to send email using SMTP in C# and you could make a RAZOR replica of it.

http://forums.asp.net/t/1602115.aspx

Hope it helps.

Thanks

Solution 2:[2]

Find the below link which will help you to send mail from UMBRACO. Send email in razor macro

http://our.umbraco.org/forum/developers/razor/22536-Send-email-in-razor-macro

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jigar Pandya
Solution 2 BJ Patel