Below is some ASP.NET C# code you can use to control your mailing lists.
using System.Net.Mail;
/// <summary>
/// Sends mail to specified list
/// </summary>
/// <param name="emailAddress"></param>
private bool SendToServergridLists(string mailToAddress, string mailFromAddress)
{
// Sending to the lists is easy.
// If you are having people subscribe/unsubscribe via
// web forms, make sure that you have the
// subscribe witout confirmation and unsubscribe without confirmation
// boxes checked in the properties of your mailing list
// in H-SPHERE. Also, you will probably want to enable sending
// from the mailing list address.
//
// this example can be used in any case, to subscribe, unsubscribe,
// or send to the list
// set mail server
SmtpClient cli = new SmtpClient("localhost"); // define the mail server
// if you are on Servergrid
// use localhost
MailMessage myMessage= new MailMessage(); // Create a new Mail Message object
myMessage.IsBodyHtml = false; // This isn't necessary
try
{
// set the message properties
// the TO will change depending on your intention
// to subscribe, you want your TO address to be myList-subscribe@whateveryourdomainis.xx
// your FROM should be the address you want added to the list
// to unsubscribe, you want your TO address to be myList-unsubscribe@whateveryourdomainis.xx
// your FROM should be the address you want removed from the list
// to send an email to the people on the list you want your TO and FROM to be myList@whateveryourdomainis.xx
myMessage.To.Add(new MailAddress(mailToAddress));
myMessage.From = new MailAddress(mailFromAddress);
myMessage.Body = string.Format("This is the message body");
myMessage.Subject = "This is the message subject";
// These next lines are essential to sending a message TO your list from a page
// add a header describing the sending agent
myMessage.Headers.Add("User-Agent", "ASP.NET");
// define the sender, this is different than "From"
myMessage.Sender = new MailAddress(mailFromAddress);
// set the encoding for the message
myMessage.BodyEncoding = System.Text.Encoding.ASCII;
myMessage.SubjectEncoding = System.Text.Encoding.ASCII;
cli.Send(myMessage);
}
catch (SmtpException stmpExp)
{
throw new Exception("Mail send failed due to smtp error: " + stmpExp.Message);
}
catch (Exception exp)
{
throw new Exception("Send failed : " + exp.Message);
}
myMessage.Dispose();
return true;
}
Michael R. Czarnecki
Senior Web Developer
http://www.servergrid.comGet a quote on web development
consulting@servergrid.com