ASP.NET Web Sayfaları - WebMail Yardımcısı
WebMail Yardımcısı - Birçok kullanışlı ASP.NET Web Yardımcısından biri.
WebMail nesnesi ile bir web sayfasından kolayca e-posta gönderebilirsiniz.
WebMail Yardımcısı
WebMail Yardımcısı, SMTP (Basit Posta aktarım Protokolü) kullanarak bir web uygulamasından e-posta göndermeyi kolaylaştırır.
Senaryo: E-posta Desteği
E-posta kullanımını göstermek için, destek için bir giriş sayfası oluşturacağız, kullanıcının sayfayı başka bir sayfaya göndermesine izin vereceğiz ve destek sorunu hakkında bir e-posta göndereceğiz.
İlk: Uygulama Başlangıç Sayfanızı Düzenleyin
Bu öğreticide Demo uygulamasını oluşturduysanız, aşağıdaki içeriğe sahip _AppStart.cshtml adlı bir sayfanız zaten vardır:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId",
"Email", true);
}
WebMail yardımcısını başlatmak için, AppStart sayfanıza aşağıdaki WebMail özelliklerini ekleyin:
_AppStart.cshtml
@{
WebSecurity.InitializeDatabaseConnection("Users", "UserProfile", "UserId",
"Email", true);
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password-goes-here";
WebMail.From = "[email protected]";
}
Açıklanan özellikler:
SmtpServer: E-postaları göndermek için kullanılacak SMTP sunucusunun adı.
SmtpPort: Sunucunun SMTP işlemlerini (e-postaları) göndermek için kullanacağı bağlantı noktası.
EnableSsl: Sunucunun SSL (Güvenli Yuva Katmanı) şifrelemesi kullanması gerekiyorsa doğrudur.
KullanıcıAdı: E-postayı göndermek için kullanılan SMTP e-posta hesabının adı.
Şifre: SMTP e-posta hesabının şifresi.
Gönderen: Gönderen adresinde görünecek e-posta (genellikle KullanıcıAdı ile aynıdır).
İkincisi: Bir E-posta Giriş Sayfası Oluşturun
Ardından bir giriş sayfası oluşturun ve bunu Email_Input olarak adlandırın:
Email_Input.cshtml
<!DOCTYPE html>
<html>
<body>
<h1>Request for
Assistance</h1>
<form method="post" action="EmailSend.cshtml">
<label>Username:</label>
<input type="text" name="customerEmail" />
<label>Details about the problem:</label>
<textarea name="customerRequest"
cols="45" rows="4"></textarea>
<p><input type="submit" value="Submit"
/></p>
</form>
</body>
</html>
Giriş sayfasının amacı bilgi toplamak ve ardından verileri e-posta olarak gönderebilecek yeni bir sayfaya göndermektir.
Üçüncüsü: Bir E-posta Gönderme Sayfası Oluşturun
Ardından, e-postayı göndermek için kullanılacak sayfayı oluşturun ve adını E-posta_Gönder:
Email_Send.cshtml
@{ // Read input
var customerEmail = Request["customerEmail"];
var customerRequest = Request["customerRequest"];
try
{
// Send email
WebMail.Send(to:"[email protected]", subject: "Help request from - " + customerEmail, body:
customerRequest );
}
catch (Exception ex )
{
<text>@ex</text>
}
}
WebMail Nesne Referansı - Özellikler
Properties | Description |
---|---|
SmtpServer | The name the SMTP server that will send the emails |
SmtpPort | The port the server will use to send SMTP emails |
EnableSsl | True, if the server should use SSL encryption |
UserName | The name of the SMTP account used to send the email |
Password | The password of the SMTP account |
From | The email to appear in the from address |
WebMail Nesne Referansı - Yöntemler
Method | Description |
---|---|
Send() | Sends an email message to an SMTP server for delivery |
Send() yöntemi aşağıdaki parametrelere sahiptir:
Parameter | Type | Description |
---|---|---|
to | String | The Email recipients (separated by semicolon) |
subject | String | The subject line |
body | String | The body of the message |
Ve aşağıdaki isteğe bağlı parametreler:
Parameter | Type | Description |
---|---|---|
from | String | The email of the sender |
cc | String | The cc emails (separated by semicolon) |
filesToAttach | Collection | Filenames |
isBodyHtml | Boolean | True if the email body is in HTML |
additionalHeaders | Collection | Additional headers |
Teknik veri
Name | Value |
---|---|
Class | System.Web.Helpers.WebMail |
Namespace | System.Web.Helpers |
Assembly | System.Web.Helpers.dll |
WebMail Helper'ı Başlatma
WebMail yardımcısını kullanmak için bir SMTP sunucusuna erişiminiz olmalıdır. SMTP, e-postanın "çıktı" kısmıdır. Bir web barındırıcısı kullanıyorsanız, muhtemelen SMTP sunucusunun adını zaten biliyorsunuzdur. Kurumsal bir ağda çalışıyorsanız, BT departmanınız size adı verebilir. Evde çalışıyorsanız, sıradan e-posta sağlayıcınızı kullanabilirsiniz.
Bir e-posta göndermek için ihtiyacınız olacak:
- SMTP sunucusunun adı
- Bağlantı noktası numarası (çoğunlukla 25)
- Bir e-posta kullanıcı adı
- bir e-posta şifresi
Web'inizin kökünde, _AppStart.cshtml adlı bir sayfa oluşturun (veya sayfayı düzenleyin) .
Dosyanın içine aşağıdaki kodu koyun:
_AppStart.cshtml
@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "[email protected]";
WebMail.Password = "password";
WebMail.From = "[email protected]"
}
Yukarıdaki kod, web sitesi (uygulama) her başlatıldığında çalışacaktır. WebMail Nesnenizi başlangıç değerleriyle besler .
Lütfen değiştirin:
smtp.example.com , e-postaları göndermek için kullanılacak SMTP sunucusunun adıyla.
25 , sunucunun SMTP işlemlerini (e-postaları) göndermek için kullanacağı bağlantı noktası numarasıyla.
sunucunun SSL (Güvenli Yuva Katmanı) şifrelemesi kullanması gerekiyorsa true ile false .
[email protected] ve e-posta göndermek için kullanılan SMTP e-posta hesabının adı.
SMTP e-posta hesabının şifresiyle şifre .
john@example ile gönderen adresinde görünecek e-posta.
AppStart dosyanızda WebMail nesnesini başlatmanız gerekmez, ancak WebMail.Send() yöntemini çağırmadan önce bu özellikleri ayarlamanız gerekir .