Benutzer:MovGP0/Email

aus Wikipedia, der freien Enzyklopädie
   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("you@your.address"));
    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).