Envio de E-mail com ASP.NET
Autor: Juliano Sales | Publicado: 2011-07-06T06:39:00-07:00

Criei uma class que envia e-mail com .net



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//Chama os objetos responsável por enviar e-mail
using System.Web.Mail;

namespace sgdc._appclass
{
&nbsp; &nbsp; public class Email
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; private string _mensagem;

&nbsp; &nbsp; &nbsp; &nbsp; public string Mensagem
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _mensagem; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _mensagem = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _from;

&nbsp; &nbsp; &nbsp; &nbsp; public string From
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _from; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _from = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _to;

&nbsp; &nbsp; &nbsp; &nbsp; public string To
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _to; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _to = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _assunto;

&nbsp; &nbsp; &nbsp; &nbsp; public string Assunto
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _assunto; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _assunto = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _formatoCorpo;

&nbsp; &nbsp; &nbsp; &nbsp; public string FormatoCorpo
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _formatoCorpo; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _formatoCorpo = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _body;

&nbsp; &nbsp; &nbsp; &nbsp; public string Body
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _body; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _body = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _smtpServer;

&nbsp; &nbsp; &nbsp; &nbsp; public string SmtpServer
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _smtpServer; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _smtpServer = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; private string _send;

&nbsp; &nbsp; &nbsp; &nbsp; public string Send
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _send; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _send = value; }
&nbsp; &nbsp; &nbsp; &nbsp; }

//Metodo&nbsp;responsável&nbsp;por enviar e-mail
&nbsp; &nbsp; &nbsp; &nbsp; public void Enviar(string from, string to, string assunto, string texto)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._from = from;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._to = to;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._assunto = assunto;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._body = texto;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MailMessage email = new MailMessage();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MailMessage mail = new MailMessage();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mail.To = to;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mail.From = from;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mail.Subject = assunto;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mail.Body = texto;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SmtpMail.SmtpServer = "localhost"; &nbsp;//your real server goes here
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SmtpMail.Send(mail);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this._mensagem = "Ocorreram problemas no envio do e-mail. Erro = " + ex.Message;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finally
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; }


&nbsp; &nbsp; }
}