2012-03-27 2 views
10

Gibt es ein Beispiel, das mich erklären kann E-Mails von meinem localhost-Server senden? Ich habe dieses Beispiel geschrieben, aber es funktioniert nicht der Fehler „Failure Mail-Versand“ ist.E-Mail wird in asp.net über lokalen Host-Server

SmtpClient smtpClient = new SmtpClient(); 
     smtpClient.Host = "localhost"; 
     smtpClient.Port = 25; 
     smtpClient.EnableSsl = false; 
     smtpClient.Credentials = new NetworkCredential("[email protected]", "secret"); 
     smtpClient.Send("[email protected]", "[email protected]", "Let’s eat lunch!", "Lunch at the Steak House?");//here is the error 

Und was soll ich in web.config tun? Hier

+1

Haben Sie SMTP auf localhost konfiguriert? – Habib

+0

Sie verwenden localhost und mit Anmeldeinformationen von Yahoo, ich glaube nicht, das – Habib

Antwort

19

ya gehen :) localhost-with-aspnet-without-smtp-server

Lassen Sie mich bitte wissen, ob es für Sie funktioniert so, wie Sie es brauchen zu.


Link oben funktioniert nicht, also werde ich die Antwort verbessern.

Zu Testzwecken können wir localhost wie folgt verwenden: How to Test Email Without Configure SMTP in ASP.NET

Falls der Link wieder nach unten geht, im Grunde müssen wir web.config wie folgt ändern:

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="SpecifiedPickupDirectory"> 
     <specifiedPickupDirectory pickupDirectoryLocation="C:\Mails\"/> 
     </smtp> 
    </mailSettings> 
    </system.net> 

und C# -Code

MailMessage mailMessage = new MailMessage(); 
    MailAddress fromAddress = new MailAddress("[email protected]"); 
    mailMessage.From = fromAddress; 
    mailMessage.To.Add("[email protected]"); 
    mailMessage.Body = "This is Testing Email Without Configured SMTP Server"; 
    mailMessage.IsBodyHtml = true; 
    mailMessage.Subject = " Testing Email"; 
    SmtpClient smtpClient = new SmtpClient(); 
    smtpClient.Host = "localhost"; 
    smtpClient.Send(mailMessage); 

Dies wird Ausgabe einer Datei in unserem gewünschten Verzeichnis.

+0

Nebenbei bemerkt funktioniert: Der 'System.Web.Mail.MailMessage' ist veraltet. Sie können 'System.Net.Mail.MailMessage' verwenden. – rst

2

Sie müssen die Einstellungen für den SMTP-Server in web.config angeben. Es gibt Online mehrere Beispiele (zB this)

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network" from="[email protected]"> 
      <network host="smtp.mail.com" userName="[email protected]" password="pwd" port="25"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

Dann können Sie einfach die SmtpClient-Klasse verwenden senden:

public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) 
{ 
    // Instantiate a new instance of MailMessage 
    MailMessage mMailMessage = new MailMessage(); 

    // Set the sender address of the mail message 
    mMailMessage.From = new MailAddress(from); 
    // Set the recepient address of the mail message 
    mMailMessage.To.Add(new MailAddress(to)); 

    // Check if the bcc value is null or an empty string 
    if ((bcc != null) && (bcc != string.Empty)) 
    { 
     // Set the Bcc address of the mail message 
     mMailMessage.Bcc.Add(new MailAddress(bcc)); 
    }  // Check if the cc value is null or an empty value 
    if ((cc != null) && (cc != string.Empty)) 
    { 
     // Set the CC address of the mail message 
     mMailMessage.CC.Add(new MailAddress(cc)); 
    }  // Set the subject of the mail message 
    mMailMessage.Subject = subject; 
    // Set the body of the mail message 
    mMailMessage.Body = body; 

    // Set the format of the mail message body as HTML 
    mMailMessage.IsBodyHtml = true; 
    // Set the priority of the mail message to normal 
    mMailMessage.Priority = MailPriority.Normal; 

    // Instantiate a new instance of SmtpClient 
    SmtpClient mSmtpClient = new SmtpClient(); 
    // Send the mail message 
    mSmtpClient.Send(mMailMessage); 
} 

Und rufen Sie die Funktion:

SmtpClient smtpClient = new SmtpClient(); 
smtpClient.EnableSsl = true; 

MailMessage msg = new MailMessage(); 
msg.To.Add("[email protected]"); 
msg.Subject = "test"; 
msg.Body = "test body"; 

smtpClient.Send(msg); 
+0

Sie brauchen keinen zusätzlichen SMTP-Server ...;) – walther

+0

Ich stimme zu, wenn es auf localhost installiert ist. was ist, wenn Sie einen externen Anbieter (z. B. Rackspace) verwenden? – Strillo

1
bool ret = true; 

      try 
      { 
       string _smtpServer = ConfigurationSettings.AppSettings["appEmailHost"]; 

       Web.Mail.Mail mail = new Web.Mail.Mail(_smtpServer,   
     System.Web.Mail.MailFormat.Html, System.Web.Mail.MailPriority.Normal); 
       mail._From = [email protected]; 
       mail._To = [email protected]; 
       mail._Subject = subject; 

       mail._Body = body; 
       mail.SendMail(); 
       ret = true; 
      } 
      catch(Exception exp) 
      { 
       _GravaErro(exp); 
       ret = false; 
      } 

      return ret;