2010-06-28 3 views
1

Possible en double:
Sending Email in .NET Through GmailEnvoi Gmail via C#

J'ai tant de problèmes avec l'envoi de courrier par C#. Je l'ai essayé pour toujours sur plusieurs applications et cela ne fonctionne jamais ....

Quelqu'un pourrait SVP poster un code exemple qui marque clairement où l'expéditeur et le destinataire vont et offre de l'aide avec la date SMTP ou quoi que ce soit !!

+6

Voir [Envoi d'un courriel dans .NET via Gmail] (http://stackoverflow.com/questions/32260/sending-email-in-net-through -Gmail). Cela aiderait si vous expliquiez "ça ne marche jamais" –

Répondre

2

Quelque chose comme ceci:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("[email protected]", "[email protected]", "subject", "body"); 
System.Net.Mail.SmtpClient emailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 465); 
emailClient.Credentials = new System.Net.NetworkCredential("yourgmailusername", "yourpassword"); 
emailClient.Send(message); 
2

Une partie du code que j'ai écrit il y a quelque temps pour l'envoi d'e-mails via un formulaire en ligne:

//using System.Net.Mail; 

    MailMessage msg = new MailMessage(); 

    msg.To.Add(RECIPIENT_ADDRESS); //note that you can add arbitrarily many recipient addresses 


    msg.From = new MailAddress(SENDER_ADDRESS, RECIPIENT_NAME, System.Text.Encoding.UTF8); 
    msg.Subject = SUBJECT 
    msg.SubjectEncoding = System.Text.Encoding.UTF8; 
    msg.Body = //SOME String 

    msg.BodyEncoding = System.Text.Encoding.UTF8; 
    msg.IsBodyHtml = false; 

    SmtpClient client = new SmtpClient();  

    client.Credentials = new System.Net.NetworkCredential(ADDRESS, PASSWORD); 
    client.Port = 587; 
    client.Host = "smtp.gmail.com"; 
    client.EnableSsl = true; 

    try 
    { 
     client.Send(msg); 
    } 
    catch (SmtpException ex) 
    { 
     throw; //or handle here 
    } 
0

J'ai fait cette classe pour envoyer via mon compte gmail quand dans mon dev environnement et utiliser le SMTP dans mon Web.Config en production. Essentiellement la même chose que noblethrasher avec un certain confort de déploiement.

Il y a un drapeau pour "mailConfigTest"

/// <summary> 
/// Send Mail to using gmail in test, SMTP in production 
/// </summary> 
public class MailGen 
{ 
    bool _isTest = false; 
    public MailGen() 
    { 
     _isTest = (WebConfigurationManager.AppSettings["mailConfigTest"] == "true"); 
    } 

    public void SendMessage(string toAddy, string fromAddy, string subject, string body) 
    { 
     string gmailUser = WebConfigurationManager.AppSettings["gmailUser"]; 
     string gmailPass = WebConfigurationManager.AppSettings["gmailPass"]; 
     string gmailAddy = WebConfigurationManager.AppSettings["gmailAddy"]; 


     NetworkCredential loginInfo = new NetworkCredential(gmailUser, gmailPass); 
     MailMessage msg = new MailMessage(); 
     SmtpClient client = null; 

     if (_isTest) fromAddy = gmailAddy; 

     msg.From = new MailAddress(fromAddy); 
     msg.To.Add(new MailAddress(toAddy)); 
     msg.Subject = subject; 
     msg.Body = body; 
     msg.IsBodyHtml = true; 
     if (_isTest) 
     { 
      client = new SmtpClient("smtp.gmail.com", 587); 
      client.EnableSsl = true; 
      client.UseDefaultCredentials = false; 
      client.Credentials = loginInfo; 
     } 
     else 
     { 
      client = new SmtpClient(WebConfigurationManager.AppSettings["smtpServer"]); 
     } 
     client.DeliveryMethod = SmtpDeliveryMethod.Network; 
     client.Send(msg); 

    } 
}