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
| <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Async="true" AsyncTimeout="1000" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:TextBox ID="txtSubject" runat="server" Columns="50" />
</p>
<p>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="5" Columns="60" />
</p>
<p>
<asp:Button ID="btnSend" runat="server" Text="Send Email"
onclick="btnSend_Click" /></p>
<p>
<asp:Label ID="labMessage" runat="server" /></p>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
SendMail();
}
/// <summary>
/// Sends email asynchronously.
/// </summary>
private void SendMail()
{
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new MailAddress("superl@free.fr");
mailMessage.To.Add(new MailAddress("superl@free.fr"));
mailMessage.Subject = txtSubject.Text.Trim();
mailMessage.Body = txtBody.Text.Trim();
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpClient smtpClient = new SmtpClient("seveurSMTPbidon");
Object userState = mailMessage;
//Attach event handler for async callback
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
try
{
smtpClient.Timeout = 1000;
//Send the email asynchronously
smtpClient.SendAsync(mailMessage, userState);
}
catch (SmtpException smtpEx)
{
smtpClient.SendAsyncCancel();
labMessage.Text = smtpEx.Message;
}
catch (Exception ex)
{
labMessage.Text = ex.Message;
}
}
/// <summary>
/// Event handler for processing completion information after asynchronous email sent.
/// </summary>
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//Get UserState as MailMessage instance from SendMail()
MailMessage mailMessage = e.UserState as MailMessage;
if (e.Cancelled)
{
labMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To[0].Address;
}
if (e.Error != null)
{
labMessage.Text = "Error occured, info=" + e.Error.Message;
}
else
{
labMessage.Text = "Mail sent successfully";
}
} |
Partager