Saturday, March 9, 2013

asmx SMTP relay!

Addendum 8/14/2013: Some of what is below is questionable. See: this

 
 

Here is an asmx web service I wrote some time ago:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
   
   public WebService ()
   {
   }
   
   [WebMethod]
   public bool Attempt(string email, string html, string password, string subject)
   {
      if (password == ConfigurationSettings.AppSettings["password"])
      {
         MailMessage alert = new MailMessage("tomjaeschke@tomjaeschke.com", email);
         alert.Subject = subject;
         alert.Body = html;
         alert.IsBodyHtml = true;
         SmtpClient emailSender = new SmtpClient();
         emailSender.Send(alert);
         return true;
      } else {
         return false;
      }
   }
}

 
 

I wrote this guy for a game I started to write and ultimately abandoned. You may see the better part of the code here. It is that hexagons gunk I mentioned before. Anyways, this is a classic hack. I've seen it in another environment, not my own. You do it because you cannot send mail from your app at hand for some reason. You hack around the problem by making a web service you may call that will send emails. Yay! This bit of my Web.config file shows off how to set up a SMTP relay!

<appSettings>
   <add key="password" value="Xenu" />
</appSettings>
<connectionStrings/>
<system.net>
   <mailSettings>
      <smtp from="tomjaeschke@tomjaeschke.com">
         <network host="localhost" password="Cthulhu"/>
      </smtp>
   </mailSettings>
</system.net>

 
 

I have a corny web site at tomjaeschke.com and I uploaded a dummy, one page web form application there which does nothing but hold the web service which is more or less hidden to anyone who visits the site. (I keep notes and photos at the tomjaeschke.com hosting.) The hosting is something I have had for a while from Network Solutions. It allows for the ASP.NET 2.0 framework. The hosting is pretty ghetto. I interface with it via FTP and I cannot use MVC there, just web forms. I built the web service in Visual Studio 2008 even though I authored the example of using it below in Visual Studio 2012 and what is technically C# 4.5. I do have this hosting anyways for notes and pictures and as it supports my asmx idea I am inclined to use it. Previously, I have owned accounts at Rackspace and discountasp.net but I find that most of the code I write is just tinkering and I do not need to publish stuff out to the world and thus the accounts I have held at Rackspace and discountasp.net proved to be wastes of money. I mostly just write code at my laptop and blog of it. Well, then there is the real stuff I do at work, but that is something else. I really need email anyways and the Network Solutions account has that bundled as a service (hence I will never just turn it off to save a buck) and so I shall abuse it. (I am not going to spin my wheels learning how to set up Microsoft Exchange at my laptop. I know little of prepping email services and they can just remain a magical unknown.) Any corny code I write for myself for fun which needs to send emails shall do so in this cheapo/ghetto manner. Cool. Speaking of the stuff I do for work, this sort of solution is going on where I work too. If you cannot send mail locally why not hack around the problem?

How do I use the asmx? "TomJaeschkeMail" is the name I used instead of "com.damisam.www" or "Reference" as seen in this example. I then used the namespace like so:

using System.Web.Mvc;
using MvcApplication.TomJaeschkeMail;
namespace MvcApplication.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         WebService tomJaeschkeMail = new WebService();
         tomJaeschkeMail.Attempt("tomjaeschke@tomjaeschke.com", "foo", "Xenu", "bar");
         tomJaeschkeMail.Attempt("fevercheese@gmail.com", "baz", "Xenu", "qux");
         return View();
      }
   }
}

 
 

Addendum 3/14/2014: My reference to C# 4.5 should really be a reference to the ASP.NET 4.5 Framework which uses C# 5.0.

No comments:

Post a Comment