i want to send mail to any email address, how to do that using C#. i am working on local host.
7 Answers
System.Net.Mail.MailMessage message=new System.Net.Mail.MailMessage(
new MailAddress(EmailUsername), new MailAddress("toemailaddress"));
message.Subject = "Message Subject"; // E.g: My New Email
message.Body = "Message Body"; // E.g: This is my new email ... Kind Regards, Me
For the SMTP part, you can also use SmtpClient
:
SmtpClient client = new SmtpClient(ServerIP);
client.Credentials = new System.Net.NetworkCredential(EmailUsername, EmailPassword);
client.Send(message);
Please consider accepting some answers. A 0% accepted rate is not great.
Edited to fix the silly mistakes. Serves me right for not checking the code first.

- 25,001
- 7
- 80
- 118
-
1
-
-
I suggest an improvement just `SmtpClient client = new SmtpClient()` and then use of web.config for configuration `
You can use the SmtpClient class and call Send (or SendAsync) with a MailMessage instance. Both these classes are in the System.Net.Mail namespace.
SmtpClient's default constructor uses the configuration from your app/web.config, but you can use other constructors to specify the SMTP settings you want.
// using System.Net.Mail;
SmtpClient client = new SmtpClient();
MailMessage mm = new MailMessage()
{
Subject = "Subject here",
Body = "Body here"
};
mm.To.Add("email@tempuri.org");
mm.From = new MailMessage("from@tempuri.org");
client.Send(mm);

- 83,269
- 19
- 178
- 237
just to add that, there is a really nice website with everything you should know about System.Net:Mail namespace
it is called:
hope it helps someone like it's been helping me ever since :)

- 73,608
- 45
- 233
- 342
-
Just wanted to say thanks for the plug. I always appreciate coming across other developers who enjoyed my site. Thanks! Dave. – dave wanta Mar 01 '10 at 15:07
This is to send Email with Attachment
using System.Net;
using System.Net.Mail;
public void email_send()
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your mail@gmail.com");
mail.To.Add("to_mail@gmail.com");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("c:/textfile.txt");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}

- 23,309
- 10
- 44
- 76

- 4,202
- 12
- 55
- 77
Use this.
private static void SendMail(string subject, string content)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("YOURMAİL");
mail.To.Add("MAİLTO");
mail.Subject = subject;
mail.Body = content;
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("YOURMAİL", "YOURMAİLPASSWORD");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
catch (Exception ex)
{
}
}
And don't forget to add ---using System.Net.Mail;---
Try this...
public static void Send(string pFrom, string pSubject, string pTo, string pBody)
{
System.Net.Mail.MailMessage loMail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential loCredencial = new System.Net.NetworkCredential(MAIL_USERNAME, MAIL_PASSWORD);
loMail.To.Add(pTo);
loMail.Subject = pSubject;
loMail.From = new System.Net.Mail.MailAddress(pFrom);
loMail.IsBodyHtml = true;
loMail.Body = pBody;
System.Net.Mail.SmtpClient loSmtp = new System.Net.Mail.SmtpClient(MAIL_SMTP);
loSmtp.UseDefaultCredentials = false;
loSmtp.Credentials = loCredencial;
loSmtp.Port = MAIL_PORT;
loSmtp.Send(loMail);
}