It's impossible ?!
ive provided a detailed way to do it using System.Net.Mail namespace;
private void button1_Click(object sender, EventArgs e)
{
SmtpClient smtpserver = new SmtpClient();
smtpserver.Credentials = new NetworkCredential("email@domain", "passphrase");
smtpserver.Port = 587;
smtpserver.Host = "smtp.live.com";
smtpserver.EnableSsl = true;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@domain");
mail.To.Add("recipient@domian");
mail.Subject = "testing";
string pathTOAttachment;
string _Attachment = pathToAttachment;
Attachment oAttch = new Attachment(_Attachment);
mail.Attachments.Add(oAttch);
mail.Body = "message body";
ThreadPool.QueueUserWorkItem(delegate
{
try
{
smtpserver.Send(mail);
}
//you can get more specific in here
catch
{
MessageBox.Show("failure sending message");
}
});
}
Noteworthy (not taken into consideration in this code sample):
- some isp may impose size limitation on attachment, some make sure you check it before attempting to send the email.
- smtp hosts/port may vary, the most efficient way is to check against a regularly updated database, or let the user set them himself.
- the threading part is all about UI responsiveness, but, if the user close the main app window with the mail still sending, it'll be preempted.