2010-12-30 19 views
6

Sto tentando di inviare un'e-mail tramite GMail da ASP.Net utilizzando il codice e la configurazione di seguito. Sfortunatamente non sembra funzionare e inoltre non sta lanciando un messaggio di errore. Non c'è nulla nei registri del server o nelle cartelle della posta di IIS mail, ho anche controllato il cestino dell'indirizzo da vedere se la posta è finita lì. Qualsiasi aiuto sarebbe molto apprezzato.Invio di e-mail Asp.Net tramite gmail

C# Sezione

public void SendFeedback() 
    { 
     string emailFrom = this.Email.Text; 

     MailMessage message = new MailMessage(); 
     // here is an important part: 
     message.From = new MailAddress(emailFrom, "Mailer"); 
     // it's superfluous part here since from address is defined in .config file 
     // in my example. But since you don't use .config file, you will need it. 
     message.Subject = "www.gumpshen.com - Website Query"; 
     message.IsBodyHtml = true; 
     message.Body = string.Format(" Name = {0}, Phone = {1}", Name.Text, Phone.Text); 
     message.Body += Environment.NewLine; 
     message.Body += Environment.NewLine; 
     message.Body += ProjectDetails.Text; ; 

     var client = new SmtpClient(); 

     try 
     { 
      client.Send(message); 

Questa è la sezione di configurazione:

<system.net> 
    <mailSettings> 
    <smtp from="[email protected]" deliveryMethod="Network" > 
     <network host="smtp.gmail.com" port="587" 
     userName="[email protected]" password="myPassword"/> 
    </smtp> 
    </mailSettings> 
</system.net> 
+0

l'ho ospitato su un Virtual Private Server, può essere qualcosa a che fare con il modo in cui ho configurato posta SMTP in IIS7, ma ho appena eseguito la procedura guidata. – Burt

risposta

9

È necessario client.EnableSsl=true;

Controllare il codice da questo sito: Email via Gmail

Ecco un esempio su come inviare e-mail HTML dalla pagina ASP.NET utilizzando il proprio account Google. (Questa impostazione può essere facilmente utilizzata per inviare messaggi tramite qualsiasi altro server SMTP che richiede l'autenticazione). Nota: lo snippet di codice presuppone che nella pagina sia presente un componente Label con lblMsg denominato che mostrerà il messaggio di successo/errore all'utente che sta inviando email. (ma può essere facilmente modificato).

SmtpClient client = new SmtpClient(); 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    client.EnableSsl = true; 
    client.Host = "smtp.gmail.com"; 
    client.Port = 587; 

    // setup Smtp authentication 
    System.Net.NetworkCredential credentials = 
     new System.Net.NetworkCredential("[email protected]", "yourpassword"); 
    client.UseDefaultCredentials = false; 
    client.Credentials = credentials;     

    MailMessage msg = new MailMessage(); 
    msg.From = new MailAddress("[email protected]"); 
    msg.To.Add(new MailAddress("[email protected]")); 

    msg.Subject = "This is a test Email subject"; 
    msg.IsBodyHtml = true; 
    msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>"); 

    try 
    { 
     client.Send(msg); 
     lblMsg.Text = "Your message has been successfully sent."; 
    } 
    catch (Exception ex) 
    { 
     lblMsg.ForeColor = Color.Red; 
     lblMsg.Text = "Error occured while sending your message." + ex.Message; 
    } 
+0

https://web.archive.org/web/20130531014149/http://www.aspdotnetfaq.com/Faq/How-to-send-HTML-Email-from-ASP-NET-using-your-Gmail-account .aspx non trovato – Kiquenet