Helpful Information
 
 
Category: .Net Development
C# Email

I am developing a C# application (not web based) and do not know how to send email from a C# windows application. Does anyone have sample code?

Thanks!

Althought you are writting desktop app, you still need to add a reference to 'System.Web'

this is how I do it:

from DEVENV (ms visual studio .net)
open the project,
on menu bar, go to Project>Add Reference...
on Add Reference dialog, under .NET tab, double click on "System.Web.dll", then click on OK

now you have added a reference to System.Web

note that you must have installed the web support.

the code I usually use to send mail:


using System.Web.Mail;

/* skipped */


MailMessage msg = new MailMessage();
msg.From = "me@my.mail.server";
msg.To = "you@your.mail.server";
msg.Subject = "how to send mail using VC#.NET";
msg.Body = "this is the answer";
msg.BodyFormat = MailFormat.Text // can be MailFormat.HTML

/* you can ignore following line for encoding, .NET will use your PC default text encoding */
msg.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-2");

/* if you need attachment */
MailAttachment ma = new System.Web.Mail.MailAttachment"c:\\mydoc\docToBeAttached");
msg.Attachments.Add(ma);
/* end attachment* section */


SmtpMail.SmtpServer = "smtp.yours"; //assign smtp server here
SmtpMail.Send(msg); //this line actually fire the msg out



eazy, huh? :)


you can also use SmtpMail.Send(from, to, subject, message) to send email, but then you can't do much about encoding, message format, attachment, etc.

public static void CreateTestMessage4(string server)
{
MailAddress from = new MailAddress("ben@contoso.com");
MailAddress to = new MailAddress("Jane@contoso.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.",
to.ToString(), client.Host, client.Port);
client.Send(message);
}

Hi, I'm also working on this function.
What if I've more than one recipents?

I tried:
msgMail.To = <sender1's email add>;
msgMail.To = <sender2's email add>;
msgMail.To = <sender3's email add>;

but it doesn't seems to be working.

public static void CreateTestMessage4(string server) { MailAddress from = new MailAddress("Ben@contoso.com"); MailAddress to1 = new MailAddress("Jane@contoso.com"); MailAddress to2 = new MailAddress("Peter@contoso.com"); MailAddress to3 = new MailAddress("Linda@contoso.com");

MailMessage message = new MailMessage();
message.From = from;
message.To.Add(to1); message.To.Add(to2); message.To.Add(to3);
message.Subject = "Using the SmtpClient class."; message.Body = @"Using this feature, you can send an e-mail message from an application very easily."; SmtpClient client = new SmtpClient(server); Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.", to.ToString(), client.Host, client.Port); client.Send(message); }

public static void CreateTestMessage4(string server)
{
MailAddress from = new MailAddress("Ben@contoso.com");
MailAddress to1 = new MailAddress("Jane@contoso.com");
MailAddress to2 = new MailAddress("Peter@contoso.com");
MailAddress to3 = new MailAddress("Linda@contoso.com");
MailMessage message = new MailMessage();
message.From = from;
message.To.Add(to1);
message.To.Add(to2);
message.To.Add(to3);
message.Subject = "Using the SmtpClient class.";
message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
Console.WriteLine("Sending an e-mail message to {0} by using SMTP host {1} port {2}.", to.ToString(), client.Host, client.Port);
client.Send(message);
}

cool!
It's working. Thanks!

Email aplication to receive, compose, send and forward emails.
This is basically an email client tools that uses the google mail API to receive,
send, and forward emails.

using googlemail api

Email aplication to receive, compose, send and forward emails.
This is basically an email client tools that uses the google mail API to receive,
send, and forward emails.

using googlemail api










privacy (GDPR)