Navigation
Taschenrechner
droid
foto
internet
pc
test
untechnisch
 

ASPX sendmail

Um eine Nachricht programm- oder benutzergesteuert von einer Internet-Seite eines Windows-Servers (DotNet-Framework installiert) abzusenden, kann das folgende Beispiel dienen.

Der eigentliche Sender ist die eigene Mailbox, die man durch Mailserver, Username, Passwort am Beginn des Programms definiert.

Das Programm selbst kann auf drei Arten benutzt werden:

1. http://meinedomäne/pfad/mail.aspx

In diesem Fall meldet sich eine Eingabemaske, in der der Benutzer seine eigene E-Mail-Adresse, ein Betreff und die eigentliche Nachricht angeben kann. Abgeschickt wird mit einem Button.

2. http://meinedomäne/pfad/mail.aspx?from=emailadresse&subj=Betrifft&body=Nachrichtentext

In diesem Fall werden alle erforderliche Angaben über die Kommandozeile angegeben. Damit ist es möglich, die Seite auch von anderen Seiten des eigenen Webs zu nutzen. Damit die Seite nicht von fremden Servern genutzt werden kann, ist die Bedingung if ( (Request.UrlReferrer==null) || ( (Request.UrlReferrer!=null) && Request.UrlReferrer.ToString().Contains(strServername) ) ) eingebaut, die das verhindern soll. Den Servernamen gibt man als Servername im Kopf des Programms an.

3. http://meinedomäne/pfad/mail.aspx

Man kann auch das Formular allein auf einer eigenen Seite platzieren und die Parameter mit einem Post-Request an die Seite mail.aspx übergeben, denn die Seite mail.aspx reagiert sowohl auf QueryString- als auch auf Post-Variablen.

mail.aspx


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
<%@ Page Language="C#" %>
 
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
<title>Senden einer Nachricht</title>
<script runat="server">
    string Mailserver = ""; // mail.fiala.cc
    string Username = ""; // franz@fiala.cc
    string Passwort = ""; // Passwort
    string strFrom = "franz@fiala.cc";
    string strTo = "";
    string strCc = "";
    string strBcc = "";
    string strSubject = "Betrifft";
    string strBody = "Nachrichtentext";
     
    void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsLocal)
        {
            string strCmdFrom = Request.Params["from"]; if (!string.IsNullOrEmpty(strCmdFrom)) strFrom = strCmdFrom;
            string strCmdTo = Request.Params["to"]; if (!string.IsNullOrEmpty(strCmdTo)) strTo = strCmdTo;
            string strCmdSubject = Request.Params["subj"]; if (!string.IsNullOrEmpty(strCmdSubject)) strSubject = strCmdSubject;
            string strCmdBody = Request.Params["body"]; if (!string.IsNullOrEmpty(strCmdBody)) strBody = strCmdBody;
            if (strTo != "")
            {
                Panel_MailDialog.Visible = false;
                SendMailMessage(strFrom, strTo, strBcc, strCc, strSubject, strBody);
            }
        }
    }
    void Button_Send_Click(object sender, EventArgs e)
    {
        SendMailMessage(strFrom, TextBox_Email.Text, "", "", "Betrifft: " + TextBox_Subject.Text, "Nachricht: " + TextBox_Message.Text);
    }
    /// <summary>
    /// Sends an mail message
    /// </summary>
    /// <param name="from">Sender address</param>
    /// <param name="to">Recepient address</param>
    /// <param name="bcc">Bcc recepient</param>
    /// <param name="cc">Cc recepient</param>
    /// <param name="subject">Subject of mail message</param>
    /// <param name="body">Body of mail message</param>
    public void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body) //, Label l, Panel p)
    {
        // Instantiate a new instance of MailMessage
        MailMessage mMailMessage = new MailMessage();
        // Set the sender address of the mail message
        mMailMessage.From = new MailAddress(from);
        // Set the recepient address of the mail message
        mMailMessage.To.Add(new MailAddress(to));
        // Check if the bcc value is null or an empty string
        if ((bcc != null) && (bcc != string.Empty))
        {
            // Set the Bcc address of the mail message
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }
        // Check if the cc value is null or an empty value
        if ((cc != null) && (cc != string.Empty))
        {
            // Set the CC address of the mail message
            mMailMessage.CC.Add(new MailAddress(cc));
        }       // Set the subject of the mail message
        mMailMessage.Subject = subject;
        // Set the body of the mail message
        mMailMessage.Body = body;
        // Set the format of the mail message body as HTML
        mMailMessage.IsBodyHtml = true;
        // Set the priority of the mail message to normal
        mMailMessage.Priority = MailPriority.Normal;
        // Instantiate a new instance of SmtpClient
        SmtpClient mSmtpClient = new SmtpClient(Mailserver);
        // Send the mail message
        try
        {
            mSmtpClient.Credentials = new System.Net.NetworkCredential(Username, Passwort);
            mSmtpClient.Send(mMailMessage);
            Label_Message.Text = "Danke für die Mitteilung!";
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            Label_Message.Text = errorMessage;
        }
        Panel_MailDialog.Visible = false;
    }
</script>
 
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label_Message" runat="server"></asp:Label>
    <asp:Panel ID="Panel_MailDialog" runat="server">
        <table>
            <tr>
                <td valign="top" width="100">
                    <b>Deine E-Mail-Adresse</b>
                </td>
                <td valign="top">
                    <asp:TextBox ID="TextBox_Email" Width="300px" runat="server" Enabled="True"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td valign="top" width="100">
                    <b>Betrifft</b>
                </td>
                <td valign="top">
                    <asp:TextBox ID="TextBox_Subject" runat="server" Width="300px" Enabled="True"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td valign="top" width="100">
                    <b>Deine Nachricht</b>
                </td>
                <td valign="top">
                    <asp:TextBox ID="TextBox_Message" runat="server" Height="230px" TextMode="MultiLine"
                        Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td valign="top" width="100">
                    <b></b>
                </td>
                <td valign="top">
                    <asp:Button ID="Button_Send" Width="300px" runat="server" OnClick="Button_Send_Click"
                        Text="Nachricht senden" />
                </td>
            </tr>
        </table>
    </asp:Panel>
    </form>
</body>
</html>

Updated on Nov 13, 2012 by Franz Fiala (Version 5)