-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempMail.cs
168 lines (133 loc) · 5.09 KB
/
TempMail.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Yove.Http.Proxy;
using System.Security.Cryptography;
using System.Text;
using Yove.Http;
using Yove.Mail.Models;
using static Yove.Mail.Settings;
namespace Yove.Mail
{
public delegate void EmailAction(Message Message);
public class TempMail : IDisposable
{
public event EmailAction NewMessage;
public List<string> Domains = new List<string>();
public List<Message> Messages
{
get
{
return AllMessages;
}
}
public string Address { get; set; }
public ProxyClient Proxy { get; set; }
public bool IsDisposed { get; private set; }
public TempMail()
{
JToken Response = Client.GetJson("https://api4.temp-mail.org/request/domains/format/json").GetAwaiter().GetResult();
foreach (JValue Domain in Response)
Domains.Add((string)Domain);
}
public string Set(string Login, string Domain)
{
if (string.IsNullOrEmpty(Login) || !Domain.Contains("@"))
throw new ArgumentException("Email invalid.");
Address = $"{Login.ToLower()}{Domain}";
Hash = CreateMD5(Address);
WaitMessage();
return Address;
}
public string SetRandom()
{
Address = $"{HttpUtils.RandomString(10).ToLower()}{Domains[new Random().Next(0, Domains.Count - 1)]}";
Hash = CreateMD5(Address);
WaitMessage();
return Address;
}
private async void WaitMessage()
{
while (!IsDisposed)
{
try
{
HttpResponse Response = await Client.Get($"https://api4.temp-mail.org/request/mail/id/{Hash}/format/json");
if (Response.Body.Contains("There are no emails yet"))
continue;
foreach (JObject Message in Response.Json)
{
if (Messages.FirstOrDefault(x => x.Id == (string)Message["mail_id"]) != null)
continue;
Message Msg = new Message
{
Id = (string)Message["mail_id"],
From = (string)Message["mail_from"],
Subject = (string)Message["mail_subject"],
TextBody = (string)Message["mail_text"],
HtmlBody = (string)Message["mail_html"],
Date = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds((double)Message["mail_timestamp"])
};
foreach (JObject File in Message["mail_attachments"])
{
Msg.Attachments.Add(new Attachment
{
Filename = (string)File["filename"],
Size = (long)File["size"],
MimeType = (string)File["mimetype"],
Url = $"https://api4.temp-mail.org/request/one_attachment/id/{Msg.Id}/{(int)File["_id"]}/format/json"
});
}
if (NewMessage != null && Msg.Date.AddMinutes(2) > DateTime.UtcNow)
NewMessage(Msg);
Messages.Add(Msg);
}
}
catch
{
//? Ignore
}
finally
{
await Task.Delay(5000);
}
}
}
public Message GetMessage(string Id)
{
if (IsDisposed)
throw new ObjectDisposedException("This object disposed.");
return Messages.FirstOrDefault(x => x.Id == Id);
}
public async Task Delete()
{
await Client.Get($"https://api4.temp-mail.org/request/delete_address/id/{Hash}/format/json");
Dispose();
}
public static string CreateMD5(string Input)
{
using (MD5 MD5 = MD5.Create())
{
byte[] HashBytes = MD5.ComputeHash(Encoding.ASCII.GetBytes(Input));
StringBuilder Builder = new StringBuilder();
for (int i = 0; i < HashBytes.Length; i++)
Builder.Append(HashBytes[i].ToString("X2"));
return Builder.ToString().ToLower();
}
}
public void Dispose()
{
Client.Dispose();
Domains.Clear();
AllMessages.Clear();
IsDisposed = true;
}
~TempMail()
{
Dispose();
GC.SuppressFinalize(this);
}
}
}