Rolosoft.comRolosoft PayPal IPN Easy for .NET
Send IPN as email attachment
Rolosoft.com: PayPal IPN Easy for .NET

You can easily send an IPN as an XML attachment using just a few lines of code.

The following example shows how to have an IPN listener send the IPN to an email address as an XML attachment

Create and ASPX page to receive PayPal IPNs
  1. Create Email.aspx

    CopyC#
    using System;
    using System.IO;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Web.UI;
    using System.Xml.Serialization;
    
    
    
    namespace IpnEasyDemo
    {
        public partial class Email : Page
        {
    
            //Change this contant with your own email address
            private const string SendTo = "noone@nowhere.com";
    
            private const string SendFrom = "donotreply@anywhere.com";
            private const string Subject = "PayPal IPN received";
    
            private readonly Rolosoft.IpnEasy.Net.Ipn _ipnEasy;
    
            protected Email()
            {
                _ipnEasy = new Rolosoft.IpnEasy.Net.Ipn();
                _ipnEasy.TransactionReceived += IpnEasyTransactionReceived;
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.HttpMethod=="POST")
                    _ipnEasy.Validate();
    
            }
    
            private static void IpnEasyTransactionReceived(object sender, Rolosoft.IpnEasy.Net.PayPal.TransactionEventArgs e)
            {
    
                //Serialize and save the IPN to memory stream.
                var ser = new XmlSerializer(typeof(Rolosoft.IpnEasy.Net.PayPal.Transaction));
                using (var ms = new MemoryStream())
                {
                    ser.Serialize(ms, e.Transaction);
                    ms.Position = 0;
    
                    //create the attachment and set the MIME type to XML
                    var attachment = new Attachment(ms,
                        new ContentType("application/xml")
                        {
                            MediaType = MediaTypeNames.Text.Xml,
                            Name = String.Concat(e.Transaction.TransactionAndNotification.TxnId, ".xml")
                        });
    
                    //create the mail message
                    var message = new MailMessage(SendFrom, SendTo,
                                                  Subject,
                                                  String.Concat("PayPal IPN received at ", DateTime.Now.ToString("R")));
                    message.Attachments.Add(attachment);
                    //send the email
                    //Note: setup your SMTP server mail credentials in web.config <system.net><mailSettings></mailSettings></system.net> section
                    var client = new SmtpClient();
                    client.Send(message);
    
                    //Close the memory stream and clean up resources
                    ms.Close();
                }
    
            }
    
        }
    }
    CopyVB.NET
    Imports System.IO
    Imports System.Net.Mail
    Imports System.Net.Mime
    Imports System.Web.UI
    Imports System.Xml.Serialization
    
    
    
    Namespace IpnEasyDemo
        Public Partial Class Email
            Inherits Page
    
            'Change this contant with your own email address
            Private Const SendTo As String = "noone@nowhere.com"
    
            Private Const SendFrom As String = "donotreply@anywhere.com"
            Private Const Subject As String = "PayPal IPN received"
    
            Private ReadOnly _ipnEasy As Rolosoft.IpnEasy.Net.Ipn
    
            Protected Sub New()
                _ipnEasy = New Rolosoft.IpnEasy.Net.Ipn()
                AddHandler _ipnEasy.TransactionReceived, AddressOf IpnEasyTransactionReceived
            End Sub
    
            Protected Sub Page_Load(sender As Object, e As EventArgs)
                If Request.HttpMethod = "POST" Then
                    _ipnEasy.Validate()
                End If
    
            End Sub
    
            Private Shared Sub IpnEasyTransactionReceived(sender As Object, e As Rolosoft.IpnEasy.Net.PayPal.TransactionEventArgs)
    
                'Serialize and save the IPN to memory stream.
                Dim ser = New XmlSerializer(GetType(Rolosoft.IpnEasy.Net.PayPal.Transaction))
                Using ms = New MemoryStream()
                    ser.Serialize(ms, e.Transaction)
                    ms.Position = 0
    
                    'create the attachment and set the MIME type to XML
                    Dim attachment = New Attachment(ms, New ContentType("application/xml") With { _
                        Key .MediaType = MediaTypeNames.Text.Xml, _
                        Key .Name = [String].Concat(e.Transaction.TransactionAndNotification.TxnId, ".xml") _
                    })
    
                    'create the mail message
                    Dim message = New MailMessage(SendFrom, SendTo, Subject, [String].Concat("PayPal IPN received at ", DateTime.Now.ToString("R")))
                    message.Attachments.Add(attachment)
                    'send the email
                    'Note: setup your SMTP server mail credentials in web.config <system.net><mailSettings></mailSettings></system.net> section
                    Dim client = New SmtpClient()
                    client.Send(message)
    
                    'Close the memory stream and clean up resources
                    ms.Close()
                End Using
    
            End Sub
    
        End Class
    End Namespace