81 lines
2.2 KiB
C#
81 lines
2.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using ln.http.route;
|
|
using MimeKit;
|
|
|
|
namespace ln.http.helpers
|
|
{
|
|
|
|
public class HttpMailer : HttpRoute, IDisposable
|
|
{
|
|
private HttpRouter _httpRouter;
|
|
private HttpCaptcha _httpCaptcha;
|
|
private HttpMailerOptions _httpMailerOptions;
|
|
private HttpMailerEndpoints _httpMailerEndpoints;
|
|
|
|
public HttpMailer(HttpRouter httpRouter, string mapPath, HttpMailerOptions mailerOptions)
|
|
: this(httpRouter, mapPath, mailerOptions, null)
|
|
{
|
|
}
|
|
|
|
public HttpMailer(HttpRouter httpRouter, string mapPath, HttpMailerOptions mailerOptions,
|
|
HttpCaptcha httpCaptcha)
|
|
: base(HttpMethod.ANY, mapPath)
|
|
{
|
|
_httpRouter = httpRouter;
|
|
_httpCaptcha = httpCaptcha;
|
|
_httpMailerOptions = mailerOptions;
|
|
_httpMailerEndpoints = new HttpMailerEndpoints(this);
|
|
_routerDelegate = _httpMailerEndpoints.RouteRequest;
|
|
|
|
httpRouter.Map(this);
|
|
}
|
|
|
|
public class HttpMailerOptions
|
|
{
|
|
public string Hostname { get; set; } = "localhost";
|
|
public int Port { get; set; } = 25;
|
|
|
|
public string? Username { get; set; }
|
|
public string? Password { get; set; }
|
|
|
|
public SmtpEncryption Encryption { get; set; } = SmtpEncryption.None;
|
|
}
|
|
|
|
class HttpMailerEndpoints : HttpEndpointController
|
|
{
|
|
private HttpMailer _httpMailer;
|
|
|
|
public HttpMailerEndpoints(HttpMailer httpMailer)
|
|
{
|
|
_httpMailer = httpMailer;
|
|
}
|
|
|
|
|
|
[Map(HttpMethod.POST, "/mail")]
|
|
public HttpResponse PostMail(HttpRequest httpRequest)
|
|
{
|
|
using (FileStream fs = new FileStream("/tmp/post.txt", FileMode.Create, FileAccess.Write))
|
|
httpRequest.ContentStream.CopyTo(fs);
|
|
|
|
return HttpResponse.NoContent();
|
|
}
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_httpRouter?.UnMap(this);
|
|
_httpMailerEndpoints.Dispose();
|
|
}
|
|
}
|
|
|
|
public enum SmtpEncryption
|
|
{
|
|
None,
|
|
Tls,
|
|
StartTls
|
|
}
|
|
|
|
}
|