Send email with SMTP with Asp.Net authentication without using JMail

From EN Ikoula wiki
Revision as of 03:06, 13 March 2016 by Ikbot (talk | contribs) (Created page with "<br /> This article has been created by an automatic translation software. You can view the article source :fr:Envoyer un email avec authentification SMTP avec Asp.Net sa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
⧼vector-jumptonavigation⧽ ⧼vector-jumptosearch⧽


This article has been created by an automatic translation software. You can view the article source here.

We put at your disposal the JMail component so that you can send your mails from our hostings. This component allows you to simply use SMTP authentication required for the shipment of these mails (confers following article)

However, you can perform this authentication and sending mail without use JMail.

Here is an example of code that you can use :

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
 
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    SendMail();
  }

  void SendMail()
  {
    MailMessage NetMail = new MailMessage();
    SmtpClient MailClient = new SmtpClient();

    // Serveur à partir duquel seront envoyés les mails
    string ThisHost = "localhost";

    // Nom d'utilisateur pour envoyer les mails (votre adresse mail dans le cadre de nos hébergements mutualisés)
    string ThisUsername =  @"utilisateur@mondomaine.fr";

    // Mot de passe de la boite mail utilisée plus ci-dessus
    string ThisPassword = "Osef412";

    // Port du serveur de messagerie (587 pour le serveur SMTP par défaut sur nos offre mutualisées)
    int ThisPort = 587;

    // Adresse de l'expéditeur du mail
    string EmailSender = "mondomaine.fr <utilisateur@mondomaine.fr>";

    // Destinataire du mail que vous souhaitez envoyer
    string EmailRecipient = "DUPONT ROGER <dupont@roger.com>";

    NetMail.From = new MailAddress(EmailSender);
    NetMail.To.Add(new MailAddress(EmailRecipient));
    NetMail.IsBodyHtml = false;
    NameValueCollection NVCSrvElements = Request.ServerVariables;
    string[] InstanceID = NVCSrvElements.GetValues("INSTANCE_ID");
    NetMail.Headers.Add("Message-Id", "<" + Guid.NewGuid().ToString() + "@mondomaine.fr>");
    NetMail.Headers.Add("X-Instance-ID", Convert.ToString(InstanceID[0]));

    // Sujet du mail que vous désirez envoyer
    NetMail.Subject = "Test";

    // Contenu du mail
    NetMail.Body = "Cet email a été envoyé à  " + DateTime.Now.ToLongTimeString();

    // On désactive le SSL pour l'envoi du mail
    MailClient.EnableSsl = false;

    NetworkCredential myCredentials = new NetworkCredential(ThisUsername, ThisPassword);
    MailClient.Credentials = myCredentials;
    MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    MailClient.Host = ThisHost;
    MailClient.Port = ThisPort;
    
    try {
      MailClient.Send(NetMail);
      Response.Write("Mail envoyé avec succès");
    } catch (Exception ex) {
      Response.Write("<pre>" + ex + "</pre>");
    }

    NetMail.Dispose();
    NetMail = null;
    MailClient = null;
  }
</script>



You are not allowed to post comments.