posted on Sunday, October 24, 2004 5:48 AM
by
suniljagadish
SMTP access using VC#.NET
I developed the registration website for DWDN where I had to add a small functionality- when somebody registers for an event, an automated mail was to be sent to him/her to the email id specified during registration.
The harder way out was to use sockets and talk to the SMTP server directly and take the pains of handling various conditions. The simpler alternative is System.Web.Mail. The beauty of .NET lies in its feature-rich classes and the organized way in which everythings has been made available to the end-programmer.
The code was as simple as:
using System.Web.Mail;
MailMessage m=new MailMessage();
m.To=txtemail.Text;
m.Subject="Date with .NET : Registration Acknowledgment";
m.From="xyz@gmail.com";
msg="Hello "+ mytitle + txtname.Text +"\n \n Your registration for the event: \n " + lblevent.Text + " is under process.\nYou will be receiving a confirmation mail.\n Please feel free to mail us on xyz@gmail.com if you need any clarifications. \n Regards";
m.Body=msg;
SmtpMail.SmtpServer="localhost";//fill in the name of the SMTP mail server
SmtpMail.Send(m);//send the mail message
It provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component. It can deliver mail through Win2000 SMTP mail service or any arbitrary mail server.
That’s cool enough. The site is hosted on WebMatrixHostitng and their server provides Authenticated-SMTP only. That’s because of their SPAM-control policy.
I had to add this extra bit of code into what I had done earlier.
m.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "myusername";
m.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "mypass";
Here comes the problem… I was using VS.NET 2002 and the “Fields” collection for the MailMessage class isn’t defined in 2002. :-( I had no other option but to install VS.NET 2003 and get going with it.