Come inviare e-mail utilizzando C# (it-IT)

Come inviare e-mail utilizzando C# (it-IT)


 Questo argomento è un how to.
Si prega di mantenerlo chiaro e semplice per quanto possibile. Evitare discussioni speculative, come pure un esame in profondità dei meccanismi sottostanti o delle tecnologie correlate.

Procedura passo-passo:

Passo 1:
Creare un nuovo progetto in Microsoft Visual Studio 2008 (File -> New -> Project -> Visual C# -> Console Application). Denominarlo Email_Sender e specificare la posizione in cui memorizzarlo.

Passo 2:
Aggiungere un nuovo elemento al progetto appena creato (Project -> Add New Item -> Class). Specificare ‘TNV_EmailHelper’ come nome per la classe. Il codice avrà ora l'aspetto seguente.
Il namespace utilizzato è
using System.Net.Mail;


class TNV_EmailHelper
{
    private SmtpClient _sClient = new SmtpClient();
    private System.Net.NetworkCredential _smtpCreds = null;
    private string _sendTo;
    private string _sendFrom;
    private string _subject;
    private string _body;

    //Constructor
    //change Host& Port names Based on SMTP server
    public TNV_EmailHelper(string host, int port)
    {
        _sClient.Host = host;
        _sClient.Port = port;
    }

    public void SendEmail(string sendFrom, // From EmailAddress
                          string password, // Password
                          string sendTo,   // To Email Address
                          string subject,  // Subject of email
                          string body)     // Boady of email
    {
        try
        {
            _sendFrom = sendFrom;
            _sendTo   = sendTo;
            _subject  = subject;
            _body     = body;

            _smtpCreds = new System.Net.NetworkCredential(
                                                    sendFrom,
                                                    password);
            _sClient.UseDefaultCredentials = false;
            _sClient.Credentials = _smtpCreds;
            _sClient.EnableSsl = true;

            MailMessage msg = BuildMessage();
            _sClient.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private MailMessage BuildMessage()
    {
        MailMessage msg = new MailMessage();
        MailAddress to = new MailAddress(_sendTo);
        MailAddress from = new MailAddress(_sendFrom);

        msg.Subject = _subject;
        msg.Body = _body;
        msg.From = from;
        msg.To.Add(to);

        return msg;
    }
}

Step 3:
Aprire Program.cs e nel metodo Main scrivere le seguenti linee di codice per inviare l'email

Program.cs

class Program
{
    static void Main(string[] args)
    {
        TNV_EmailHelper em = new TNV_EmailHelper(
                                 "Smtp.gmail.com", 587);
        em.SendEmail("from@gmail.com", "password",
                     "to@gmail.com",
                     "SUB: From Email Sender",
                     "BODY: Hello World !");
    }
}



Altre lingue

Questo articolo è disponibile anche nelle seguenti lingue:

English (en-US)

 

Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Comments
  • Luigi Bruno edited Original. Comment: Added the "Altre lingue" section. Added the "Multi Language Wiki Articles" tag.

Page 1 of 1 (1 items)
Wikis - Comment List
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Luigi Bruno edited Original. Comment: Added the "Altre lingue" section. Added the "Multi Language Wiki Articles" tag.

Page 1 of 1 (1 items)