5

i want to send mail to any email address, how to do that using C#. i am working on local host.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Sheery
  • 1,123
  • 5
  • 14
  • 22
  • 1
    Ppsslbe duplicate of http://stackoverflow.com/questions/449887/sending-e-mail-using-c – Dykam Mar 01 '10 at 07:58

7 Answers7

8
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.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
4

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);
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
2

just to add that, there is a really nice website with everything you should know about System.Net:Mail namespace

it is called:

http://www.SystemNetMail.com/

hope it helps someone like it's been helping me ever since :)

balexandre
  • 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
0

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);
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Ranadheer Reddy
  • 4,202
  • 12
  • 55
  • 77
0

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;---

0

If you're using ASP.Net MVC I would recommend that you have a look at MvcMailer

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
noocyte
  • 2,484
  • 5
  • 29
  • 44
0

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);
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Marcos
  • 90
  • 6