dotnet sendmail

code :: sending email with c#

sending email is a snap with asp.net 2.0, this simple function sends an email to the specified name and email you pass to the function. with a little database integration this can be recalled in almost infinite recursion, and will not be tagged as spam because it sends individual emails, not a bulk message to many users. for fastest delivery times specify the direct path to your mail server (see line #18), and setup your mail server to not scan outgoing  webserver mail for spam. *edit* i now recant that statement. true, pure execution times will be faster but at the sake of ignorance. i would rather have the slightly increased send time and the knowledge that my code has not be hijacked and sending spam. 

using System.Net.Mail;
using System.Net.Mime;
 
protected void sendmsg(string name, string email)
{
    String filename = Server.MapPath("some.random.file.exe");
    Attachment file = new Attachment(filename, MediaTypeNames.Application.Octet);
 
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("sender@fontvir.us");
    msg.To.Add(new MailAddress(email));
    msg.Subject = "test email";
    msg.Attachments.Add(file);
    msg.IsBodyHtml = true;
    msg.Body = "hello, " + name.ToString();
 
    SmtpClient client = new SmtpClient();
    client.Host = "mail.fontvir.us";
    client.Send(msg);
}

example useage

sendmsg("xero", "x@xero.owns.us");