2012-10-30 23 views
5

Sto cercando di creare modulo di contatto per inviare e-mail (from e to sarà da interfaccia utente):Come inviare la posta tramite C# asp.net per modulo di contatto

try { 
    MailMessage mail = new MailMessage(); 
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
    mail.From = new MailAddress("fromadd"); 
    mail.To.Add("toadd"); 
    mail.Subject = "Test Mail"; 
    mail.Body = "This is for testing SMTP mail from GMAIL"; 
    SmtpServer.Port = 587; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("username","password"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 
    MessageBox.Show("mail Send"); 
} 
catch (Exception ex) { 
    MessageBox.Show(ex.ToString()); 
} 

Questo funziona solo per Gmail - tuttavia , Mi piacerebbe farlo funzionare per qualsiasi provider di posta elettronica - come potrei fare questo?

+1

Cosa vuol dire 'Codice delle generica? L'unica cosa che lo rende specifico per Gmail è il valore SMTP e * possibilmente * la porta. – Arran

+1

Perché non solo passare la porta e l'host come parametri per questa funzione? – Blachshma

+0

significa che devo digitare Da - yahoo id e to - gmail id .. generico – user1785946

risposta

1

Non utilizzare parametri hardcoded per la connessione al server SMTP.

Utilizzare invece il webconfig. Il tuo programma sarà più "generico". Basta modificare la configurazione in cui si desidera inviare attraverso un altro server SMTP

2

È necessario configurare la SmtpClient nel web.config:

<configuration> 
    <system.net> 
    <mailSettings> 
     <smtp deliveryMethod="network"> 
     <network 
      host="localhost" 
      port="25" 
      defaultCredentials="true" 
     /> 
     </smtp> 
    </mailSettings> 
    </system.net> 
</configuration> 

Poi nel codice che si può fare:

try 
    { 
     MailMessage mail = new MailMessage(); 
     mail.From = new MailAddress("fromadd"); 
     mail.To.Add("toadd"); 
     mail.Subject = "Test Mail"; 
     mail.Body = "This is for testing SMTP mail from GMAIL"; 

     SmtpClient SmtpServer = new SmtpClient();    
     SmtpServer.Send(mail); 
     MessageBox.Show("mail Send"); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.ToString()); 
    } 
+0

dove posso trovare client smtp – user1785946

0

Per me sembra che l'unica cosa che potrebbe impedirti di inviare l'email usando qualsiasi server di posta elettronica è il fatto che alcuni server di posta richiedono l'autenticazione (o possibilmente un numero di porta alternativo).

Ecco un po 'di codice di base che dovrebbe farti puntato nella giusta direzione

public class SendMail 
{ 

    public SendMail(string SMTPServer, string fromEmail) 
    { 
     this.SMTPServer = SMTPServer; 
     this.FromEmail = fromEmail; 
    } 

    public SendMail(string SMTPServer, string fromEmail, string Username, string Password) : this(SMTPServer, fromEmail) 
    { 
     this.Username = Username; 
     this.Password = Password; 
    } 

    public string SMTPServer { get; set; } 
    public string FromEmail { get; set; } 
    public string Username { get; set; } 
    public string Password { get; set; } 

    public void Send(string toEmail, string subject, string data) 
    { 
     MailMessage mailMsg = new MailMessage(); 
     mailMsg.To.Add(toEmail); 

     MailAddress mailAddress = new MailAddress(this.FromEmail); 

     mailMsg.From = mailAddress; 

     mailMsg.Subject = subject; 
     mailMsg.Body = data; 
     mailMsg.IsBodyHtml = true; 
     SmtpClient smtpClient = new SmtpClient(this.SMTPServer, 25); 

     if (this.Username.Length > 0 && this.Password.Length > 0) 
     { 
      System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(this.Username, this.Password); 
      smtpClient.Credentials = credentials; 
     } 

     smtpClient.Send(mailMsg); 
    } 

} 
0

sto provando questo codice.

try 
{ 
    MailMessage mail = new MailMessage(); 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 
    mail.Subject = "Accept Request"; 
    mail.Body = body; 
    mail.IsBodyHtml = true; 
    SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
    smtp.Credentials = new System.Net.NetworkCredential("[email protected]", " password"); 
    smtp.EnableSsl = true; 
    smtp.UseDefaultCredentials = true; 
    smtp.Send(mail); 
} 
catch (Exception ex) 
{ 
    ViewData.ModelState.AddModelError("_FORM", ex.ToString()); 
} 
1

Si può anche provare:

MailMessage msgObj = new MailMessage(); 

    msgObj.To = "[email protected]"; 
    msgObj.From = "Mike"; 
    msgObj.Bcc = "[email protected]"; 
    msgObj.Subject = "Test Message"; 
    msgObj.Body = "Hello World!"; 
    SmtpMail.SmtpServer = "Your Server"; 
    SmtpMail.Send("FromEmail", "ToEmail", "Subject", "Query"); 
    SmtpMail.Send(msgObj); 
+0

Mettilo in una funzione e sei pronto. – semicolon

Problemi correlati