Benutzer:MovGP0/Email

 MovGP0   Über mich   Hilfen   Artikel   Weblinks   Literatur   Zitate   Notizen   Programmierung   MSCert   Physik 


create and send Email[1]

using System.Net.Mail;
using System.IO;

public static CreateAndSendMessage(...)
{
   var file = "data.xls";
   
   // create the message
   using(var message = new MailMessage(from, to, subject, body))
   {
      message.IsBodyHtml = true;
      message.BodyEncoding = Encoding.UTF8;
      message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
      
      // create attachment
      using(var attachment = new Attachment(file, MediaTypeNames.Application.Octet))
      {
         var disposition = attachment.ContentDisposition;
         disposition.CreationDate = File.GetCreationTime(file);
         disposition.ModificationDate = File.GetLastWriteTime(file);
         disposition.ReadDate = File.GetLastAccessTime(file);
         message.Attachments.Add(attachment);
   
         // send the message.
         var smtpServerName = ConfigurationManager.AppSettings["smtpServer"];
         var port = int.Parse(ConfigurationManager.AppSettings["smtpPort"]);
         var useSsl = bool.Parse(ConfigurationManager.AppSettings["smtpSSL"]);

         using(var client = new SmtpClient(server, port))
         {
            client.EnableSsl = useSsl;
         
            // customCredentials
            client.UseDefaultCredentials = false;
            client.Credentials = CredentialCache.DefaultNetworkCredentials; // new NetworkCredential(@"DOMAIN\USER", "PASSWORD");
         
            //// DON'T: ignore invalid certificate
            // ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => true;
                       
            client.Send(message);
         }
      }
   }
}

Inline Image

using (var client = new SmtpClient())
{
    MailMessage newMail = new MailMessage();
    newMail.To.Add(new MailAddress("[email protected]"));
    newMail.Subject = "Test Subject";
    newMail.IsBodyHtml = true;

    var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png"));
    inlineLogo.ContentId = Guid.NewGuid().ToString();

    string body = string.Format(@"<img src=""cid:{0}"" />", inlineLogo.ContentId);

    var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
    view.LinkedResources.Add(inlineLogo);
    newMail.AlternateViews.Add(view);

    client.Send(newMail);
}

app.config / web.config

Der Server kann deklarativ konfiguriert werden:[2]

<configuration>
  <system.net>
    <mailSettings>
      <!-- configure SMTP server -->
      <smtp deliveryMethod="network">
        <network host="localhost" port="25" defaultCredentials="true" />
      </smtp>

      <!-- use SSL; use custom credentials -->
      <!--
      <smtp deliveryMethod="network">
        <network host="localhost" port="465" defaultCredentials="false" enableSsl="true" />
      </smtp>
      -->
      <!-- deliver mails to File System -->
      <!--
      <smtp deliveryMethod="SpecifiedPickupDirectory">  
        <specifiedPickupDirectory pickupDirectoryLocation="c:\foo\emails\" />  
      </smtp>
      -->
    </mailSettings>
  </system.net>
</configuration>

Quellen

  1. MailMessage Class. In: MSDN. Microsoft, abgerufen am 19. Mai 2014.
  2. <mailSettings> Element (Network Settings). In: MSDN. Microsoft, abgerufen am 20. Mai 2014 (englisch).

Tools

  • smtp4dev. In: CodePlex. Abgerufen am 20. Mai 2014 (englisch, SMTP Dummy Server).

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.