C# Pošalji mail

C# Pošalji mail

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon

Ovaj program šalje mail preko gmaila…
U kodu morate dodati koji je vaš mail koji je mail na koji želite poslati mail, subjekt koji će se prikazati u poruci i text koji će pisati u mailu …
Pazite da van netko ne ukrade program da nebi došao do vašeg passworda :)

using System;
using System.Net;
using System.Net.Mail;

namespace SendMailGmailConsole
{
   class Program
   {
   static void Main(string[] args)
   {

      //Specify senders gmail address
      string SendersAddress = "vas_mail@gmail.com";
      //Specify The Address You want to sent Email To(can be any valid email address)
      string ReceiversAddress = "primateljev_mail@gmail.com";
      //Specify The password of gmial account u are using to sent mail(pw of sender@gmail.com)
      const string SendersPassword = "Vas pass na gmailu";
      //Write the subject of ur mail
      const string subject = "Subjekt";
      //Write the contents of your mail
      const string body = "Text";

      try
      {
        //we will use Smtp client which allows us to send email using SMTP Protocol
        //i have specified the properties of SmtpClient smtp within{}
        //gmails smtp server name is smtp.gmail.com and port number is 587
        SmtpClient smtp = new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials = new NetworkCredential(SendersAddress, SendersPassword),
           Timeout = 5000
        };

        //MailMessage represents a mail message
        //it is 4 parameters(From,TO,subject,body)

        MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body);
        /*WE use smtp sever we specified above to send the message(MailMessage message)*/

        smtp.Send(message);
        Console.WriteLine("Poruka uspješno poslana");
        Console.ReadKey();
     }
     catch (Exception ex)
     {
        Console.WriteLine(ex.Message);
        Console.ReadKey();
     }
}
}
}


Podijeli