Demostracion de C#
Enviado por Flerida Garcia • 1 de Octubre de 2015 • Apuntes • 875 Palabras (4 Páginas) • 70 Visitas
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web.Configuration;
namespace com.ooit.edu {
public class MailService {
private static MailService instance;
// get singleton
public static MailService GetInstance() {
return instance == null ? instance = new MailService() : instance;
}
// eg @"~\web.config"
public static string WEB_CONFIG_PATH { get; set; }
private MailService() { }
// send regular mail
public void SendMail(string from, IList<string> tos, string subject, string text,
string smtpHost, string userid, string passwd) {
// create message
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
foreach (string to in tos) {
msg.To.Add(new MailAddress(to));
}
msg.Subject = subject;
msg.Body = text;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;
// connect to smtp server
NetworkCredential SMTPUserInfo = new NetworkCredential(userid, passwd);
SmtpClient c = new SmtpClient(smtpHost);
c.Credentials = SMTPUserInfo;
// send
c.Send(msg);
}
// send mail within the context of ASP.NET
public void SendMailInASP(IList<string> tos, string subject, string text) {
// read configuration file usually : @"~\web.config"
Configuration config = WebConfigurationManager.OpenWebConfiguration(WEB_CONFIG_PATH);
MailSettingsSectionGroup settings =
(MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
MailMessage msg = new MailMessage();
msg.From = new MailAddress(settings.Smtp.From);
...