Enviando email com anexo

<< Click to Display Table of Contents >>

Navigation:  ASP.NET > Email >

Enviando email com anexo

Previous pageReturn to chapter overviewNext page

Tela

 

email1

 

Gmail

 

email2

 

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!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></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

   

      <asp:Label ID="Label1" runat="server" Text="Nome:"></asp:Label>

      <br />

      <asp:TextBox ID="txtNome" runat="server" Width="216px">Juju</asp:TextBox>

      <br />

      <asp:Label ID="Label2" runat="server" Text="Email:"></asp:Label>

      <br />

      <asp:TextBox ID="txtEmail" runat="server" Width="219px">flavio.junior@gmail.com</asp:TextBox>

      <br />

      <asp:Label ID="Label4" runat="server" Text="Mensagem:"></asp:Label>

      <br />

      <asp:TextBox ID="txtMsg" runat="server" Columns="30" Height="78px"

          MaxLength="10" TextMode="MultiLine"></asp:TextBox>

      <br />

      <asp:Label ID="Label3" runat="server" Text="Anexo:"></asp:Label>

      <br />

      <asp:FileUpload ID="fuAnexo" runat="server" />

      <br />

      <br />

      <asp:Button ID="btnEnviar" runat="server" Text="Enviar"

          onclick="btnEnviar_Click" />

  </div>

  </form>

</body>

</html>

 

 

Default.aspx.cs

 

using System;

using System.Net.Mail;

using System.Net;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

  protected void Page_Load(object sender, EventArgs e)

   {

 

   }

 

  protected void btnEnviar_Click(object sender, EventArgs e)

   {

      MailMessage mail = new MailMessage();

       mail.From = new MailAddress("mulajunior@yahoo.com.br", "Mulinha");

       mail.To.Add(txtEmail.Text);

       mail.Subject = "Veja anexo";

       mail.IsBodyHtml = true;

       mail.Body = txtNome.Text + "<br/>" + txtMsg.Text;

 

      // anexo

      MemoryStream ms = new MemoryStream(fuAnexo.FileBytes);

      Attachment anexo = new Attachment(ms, fuAnexo.FileName);

       mail.Attachments.Add(anexo);

 

      // credenciais

      SmtpClient sc = new SmtpClient("smtp.mail.yahoo.com.br");

       sc.Credentials = new NetworkCredential("mulajunior", "animal");

 

      // manda

       sc.Send(mail);

   }

}