Asp.Net(C#)送信Emailの使い方
私たちはよくwebプログラムを書いてメールを送る機能を使います.ここでは届出をします.実はこの機能は簡単で、まずWebでconfigのに追加:
プログラムコード:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]" >
<network host="localhost" defaultCredentials="false" userName="[email protected]" password="passowrd"/>
</smtp>
</mailSettings>
</system.net>
プログラムコード:
SmtpSection cfg = ConfigurationManager.GetSection(@"system.net/mailSettings/smtp") as SmtpSection;
SmtpNetworkElement smtElement = cfg.Network;
//
MailMessage email;
string displayName="xxxx"; //email
if (string.IsNullOrEmpty(displayName))
{
email = new MailMessage(_FromEmail, _ToEmail);
}
else
{
MailAddress fromMail = new MailAddress(_FromEmail, displayName, System.Text.Encoding.UTF8);
MailAddress toMail = new MailAddress(_ToEmail);
email = new MailMessage(fromMail, toMail);
//email.Sender = new MailAddress(_FromEmail, displayName, System.Text.Encoding.UTF8);
}
email.IsBodyHtml = isHtml;
email.Body = _Body;
email.Subject = _Subject;
email.BodyEncoding = System.Text.Encoding.UTF8;
// smtp ,
System.Net.Mail.SmtpClient stmp = new SmtpClient(smtElement.Host);
// , credentials
if (smtElement.DefaultCredentials)
{
stmp.UseDefaultCredentials = true;
stmp.Credentials = new System.Net.NetworkCredential(smtElement.UserName, smtElement.Password);
}
//
stmp.Send(email);