Mar 29, 2007

Sending Email thru C#

Sending email thru C#, .NET 2.0 , with attachment,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;

namespace MailConnect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Sendbutton_Click(object sender, EventArgs e)
{
MailMessage aMessage = new MailMessage("matespoint@gmail.com", "xyz@yahoo.com");
aMessage.Subject = "This is test message";
aMessage.Body = "Body text message";

Attachment aT = new Attachment("C:\files.doc");
aMessage.Attachments.Add(aT);
aT = new Attachment("C:\files.pdf");
aMessage.Attachments.Add(aT);

SmtpClient aClient = new SmtpClient(servername, 25);
aClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
aClient.Send(aMessage);
MessageLabel.Text = "Mail has been sent successfully";
}
}
}