Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net library #36

Open
jaycollett opened this issue Aug 16, 2022 · 1 comment
Open

.net library #36

jaycollett opened this issue Aug 16, 2022 · 1 comment
Labels
library For requests regarding XARF libraries

Comments

@jaycollett
Copy link

I searched around on the internet looking for a .net compatible NuGet/library for this and came up empty. I'd be interested in building one if you were not already aware of someone doing so?

@FrederikP FrederikP added the library For requests regarding XARF libraries label Nov 4, 2022
@NXTwoThou
Copy link

FYI, I put in XARF into my in-house security software to reduce my daily workload of filling out DigitalOcean reports. It was pretty simple. Tossed some of the json samples into https://json2csharp.com/, filled out the structure, serialized it out into a MemoryStream.

The only frustrating thing is I couldn't set up feedback-report with System.Net.Mail. Had to use MailKit. Putting this here in case anyone else kept fighting with System.Net.Mail.

`
using MailKit;
using MimeKit;

public class Xarf
{
string userAgent = "MySoftware/1.0";
string myMailServer="mail.MySoftware.duh";
string myMailUserName="MyUser";
string myMailPassword="MyEasyToGuessPassword";

public void SendEmailXARF(string toEmail, string fromEmail, string subject, string body, MemoryStream memoryStreamXARF)
{
    var message = new MimeMessage();

    #region Primary headers
    message.From.Add(new MailboxAddress("", fromEmail));
    message.To.Add(new MailboxAddress("", toEmail));
    message.Subject = subject;
    #endregion

    #region Build multiparts
    var multipartBody = new TextPart("plain") { Text = body };
    var memoryStreamFeedbackType = new MemoryStream(ASCIIEncoding.ASCII.GetBytes("Feedback-Type: xarf\nUser-Agent: " + userAgent + "\nVersion: 1"));
    var multipartFeedbackType = new MimePart("message", "feedback-report")
    {
        Content = new MimeContent(memoryStreamFeedbackType),
        ContentDisposition = new ContentDisposition(ContentDisposition.Inline)
    };
    var multipartAttachmentXARF = new MimePart("application", "json")
    {
        Content = new MimeContent(memoryStreamXARF),
        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
        ContentTransferEncoding = ContentEncoding.Base64,
        FileName = "xarf.json"
    };
    #endregion

    #region Add multiparts
    var multipart = new Multipart("report; report-type=feedback-report");
    multipart.Add(multipartBody);
    multipart.Add(multipartFeedbackType);
    multipart.Add(multipartAttachmentXARF);
    message.Body = multipart;
    #endregion

    #region Send email
    using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
    {
        smtpClient.Connect(myMailServer);
        smtpClient.Authenticate(myMailUserName, myMailPassword);
        smtpClient.Send(message);
        smtpClient.Disconnect(true);
    }
    #endregion
    memoryStreamFeedbackType.Dispose();
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library For requests regarding XARF libraries
Development

No branches or pull requests

3 participants