-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
smtp.aspx.cs
109 lines (102 loc) · 4.14 KB
/
smtp.aspx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.IO;
public partial class smtp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
string strFrom = Request.QueryString["From"];
string strTo = Request.QueryString["To"];
string strSubject = Request.QueryString["Subject"];
string strBody = Request.QueryString["Body"];
string strHost = Request.QueryString["Host"];
string strUsername = Request.QueryString["Username"];
string strPassword = Request.QueryString["Password"];
string strAction = Request.QueryString["Action"];
string strSecureToken = Request.QueryString["SecureToken"];
string strAttachment = Request.QueryString["Attachment"];
switch (strAction)
{
case "Send":
var strStatus = "OK";
if (String.IsNullOrEmpty(strAttachment))
{
strStatus = SendEmail( strUsername,
strPassword,
strHost,
strTo,
strFrom,
strSubject,
strBody);
}
else
{
strStatus = SendEmailWithAttachment(strUsername,
strPassword,
strHost,
strTo,
strFrom,
strSubject,
strBody,
new Uri(strAttachment));
}
Response.Write(strStatus);
break;
}
}
private string SendEmail(string Username, string Password, string Host, string To, string From, string Subject, string Body)
{
MailMessage mail = new MailMessage(From, To);
SmtpClient client = new SmtpClient();
client.Port = 25;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
client.Host = Host;
mail.Subject = Subject;
mail.Headers.Add("X-PLEASE-REPORT-ANY-SPAM", "Is this spam?, please report to http://www.smtpjs.com/#spam");
mail.Body = Body;
try
{
client.Send(mail);
return "OK";
}
catch(Exception ex)
{
return ex.Message;
}
}
private string SendEmailWithAttachment(string Username, string Password, string Host, string To, string From, string Subject, string Body, Uri Attachment)
{
WebClient wc = new WebClient();
var bAttachment = wc.DownloadData(Attachment);
var strName = Attachment.Segments.Last();
MailMessage mail = new MailMessage(From, To);
SmtpClient client = new SmtpClient();
client.Port = 25;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
client.Host = Host;
mail.Subject = Subject;
mail.Headers.Add("X-PLEASE-REPORT-ANY-SPAM", "Is this spam?, please report to http://www.smtpjs.com/#spam");
mail.Body = Body;
mail.Attachments.Add(new Attachment(new MemoryStream(bAttachment), strName));
try
{
client.Send(mail);
return "OK";
}
catch(Exception ex)
{
return ex.Message;
}
}
}